Interview Question :
convers different data type in a list to string with python
get the largest number from the list
my_list = [1, 2.5, True, 'hello', None] find the largest number using python
Second largest number in the list:
remove repeated numbers in a list
remove repeated numbers in two different list:
list1 = [1, 2, 2.5, 3, 4, 4]
list2 = [2.5, 3, 5, 6, 2]
Method 1:
# Remove duplicates from each list
unique_list1 = []
[unique_list1.append(item) for item in list1 if item not in unique_list1]
unique_list2 = []
[unique_list2.append(item) for item in list2 if item not in unique_list2]
print("Unique List 1:", unique_list1)
print("Unique List 2:", unique_list2)
method 2 :
list1 = [1, 2, 2.5, 3, 4, 4]
list2 = [2.5, 3, 5, 6, 2]
# Combine both lists, convert to a set to remove duplicates, then back to lists
combined_list = list1 + list2
unique_combined_list = list(set(combined_list))
# Create lists with unique elements from each original list
unique_list1 = [item for item in unique_combined_list if item in list1]
unique_list2 = [item for item in unique_combined_list if item in list2]
print("Unique List 1:", unique_list1)
print("Unique List 2:", unique_list2)
remove repeated characters in a string :
input_string = "programming"
# Initialize an empty string to store the result
result = ""
# Use a set to track characters that have already appeared
seen = set()
# Iterate through the string
for char in input_string:
if char not in seen:
result += char
seen.add(char)
print(result)
count the character in a string and store in key value pair
Method 1: Using a Dictionary
input_string = "programming"
# Initialize an empty dictionary to store character counts
char_count = {}
# Loop through each character in the string
for char in input_string:
if char in char_count:
# If the character is already in the dictionary, increment its count
char_count[char] += 1
else:
# If the character is not in the dictionary, add it with a count of 1
char_count[char] = 1
print(char_count)
remove the vowels in a string
Method 2: Using a For Loop
input_string = "programming"
# Define vowels
vowels = "aeiouAEIOU"
# Initialize an empty string to store the result
result = ""
# Loop through each character in the input string
for char in input_string:
if char not in vowels:
result += char
print(result)
Replace character with the string
input_string = "programming"
# Define vowels
vowels = "aeiouAEIOU"
# Replace each vowel with '*'
for vowel in vowels:
input_string = input_string.replace(vowel, '*')
print(input_string)
convert mixed case characters
count specific character using different method
Method 2: Using a for loop
different set operation with example
1. Union (| or union())
2. Intersection (& or intersection())
Summary on sets:
A = {1, 2, 3}
B = {3, 4, 5}
# Union
print(A | B) # {1, 2, 3, 4, 5}
# Intersection
print(A & B) # {3}
# Difference
print(A - B) # {1, 2}
# Symmetric Difference
print(A ^ B) # {1, 2, 4, 5}
# Subset
print(A <= B) # False
# Proper Subset
print(A < B) # True
# Cartesian Product
import itertools
print(set(itertools.product(A, B))) # {(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)}
# Complement (given Universal set)
U = {1, 2, 3, 4, 5}
print(U - A) # {4, 5}