-
Write a program to count the frequency of each character in a given string using a dictionary.
Sample data: "hello"
Hint: Iterate through each character in the string and update the count in the dictionary.
-
Write a program to merge two dictionaries into a single dictionary.
Sample data: dict1 = {"a": 1, "b": 2} dict2 = {"c": 3, "d": 4}
Hint: Use the
update()method to merge the dictionaries.
-
Write a program to find the keys with the maximum value in a dictionary.
Sample data: {"a": 10, "b": 5, "c": 10}
Hint: Use the
max()function with a custom key argument to find the maximum value.
-
Write a program to check if a given key exists in a dictionary.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the
inoperator to check for key existence.
-
Write a program to remove a specific key from a dictionary.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the
pop()method to remove the key.
-
Write a program to find the common keys in two dictionaries.
Sample data: dict1 = {"a": 1, "b": 2, "c": 3} dict2 = {"b": 3, "c": 4, "d": 5}
Hint: Use the
keys()method to get the keys and compare them.
-
Write a program to get the length of a dictionary.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the
len()function to calculate the length.
-
Write a program to sort a dictionary by its keys in ascending order.
Sample data: {"b": 2, "a": 1, "c": 3}
Hint: Use the
sorted()function with a custom key argument to sort the dictionary.
-
Write a program to find the key with the minimum value in a dictionary.
Sample data: {"a": 10, "b": 5, "c": 15}
Hint: Use the
min()function with a custom key argument to find the minimum value.
-
Write a program to get the values of a dictionary as a list.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the
values()method to get the values and convert them into a list.
-
Write a program to check if a given value exists in a dictionary.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the
values()method and the
inoperator to check for value existence.
-
Write a program to create a dictionary with keys as numbers from 1 to 10 and their squares as values.
Sample data: keys: [1, 2, 3, ..., 10] values: [1, 4, 9, ..., 100]
Hint: Use a for loop and the dictionary's indexing to create the dictionary.
-
Write a program to find the difference between two dictionaries.
Sample data: dict1 = {"a": 1, "b": 2, "c": 3} dict2 = {"b": 2, "c": 3, "d": 4}
Hint: Use the keys() and items() methods to find the difference between the dictionaries.
-
Write a program to remove all the items from a dictionary.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the clear() method to remove all items from the dictionary.
-
Write a program to convert a dictionary into a list of tuples.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use the items() method to convert the dictionary into a list of tuples.
-
Write a program to check if all the values in a dictionary are unique.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Convert the dictionary values into a set and compare the lengths.
-
Write a program to extract all the values from nested dictionaries into a single list.
Sample data: {"a": 1, "b": {"c": 2, "d": 3}, "e": 4}
Hint: Use recursion to iterate through the nested dictionaries and extract the values.
-
Write a program to find the key-value pairs that have the highest values in a dictionary.
Sample data: {"a": 10, "b": 5, "c": 10}
Hint: Use the items() method and the max() function with a custom key argument to find the highest values.
-
Write a program to create a dictionary from two lists, one containing keys and the other containing values.
Sample data: keys = ["a", "b", "c"] values = [1, 2, 3]
Hint: Use the zip() function to combine the two lists into key-value pairs.
-
Write a program to iterate over a dictionary and print the keys and values.
Sample data: {"a": 1, "b": 2, "c": 3}
Hint: Use a for loop and the items() method to iterate over the dictionary and print the keys and values.