Skip to content

Files

Latest commit

4215bbd · Jun 23, 2023

History

History
134 lines (109 loc) · 4.46 KB

dictionary.md

File metadata and controls

134 lines (109 loc) · 4.46 KB

dictionary

  1. 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.

  2. 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.

  3. 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.

  4. Write a program to check if a given key exists in a dictionary.

    Sample data: {"a": 1, "b": 2, "c": 3}
    

    Hint: Use the in operator to check for key existence.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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 thein operator to check for value existence.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. 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.