Python – Dictionary Exercises

Here are some Python dictionary exercises to help practice your understanding and improve your skills with dictionaries. These exercises will cover a variety of common tasks you might need to perform when working with dictionaries.


Exercise 1: Create a Dictionary from Two Lists

You have two lists: one of keys and one of values. Create a dictionary by combining them.

keys = ['name', 'age', 'city']
values = ['Alice', 25, 'New York']

# Expected Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}

Exercise 2: Merge Two Dictionaries

Given two dictionaries, merge them into one. If there are overlapping keys, the values from the second dictionary should override those from the first.

dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Expected Output: {'a': 1, 'b': 3, 'c': 4}

Exercise 3: Check for Key in Dictionary

Write a function that checks whether a specific key exists in a dictionary.

my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

# Check if 'age' exists in the dictionary
# Expected Output: True

Exercise 4: Count Occurrences of Items in a List and Store in Dictionary

You are given a list of strings. Write a program to count how many times each string appears in the list, and store the result in a dictionary.

my_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']

# Expected Output: {'apple': 3, 'banana': 2, 'orange': 1}

Exercise 5: Invert a Dictionary

Write a function that takes a dictionary and returns a new dictionary where the keys become the values and the values become the keys.

original_dict = {'a': 1, 'b': 2, 'c': 3}

# Expected Output: {1: 'a', 2: 'b', 3: 'c'}

Exercise 6: Find the Maximum Value in a Dictionary

Given a dictionary of numbers, write a program to find the key with the maximum value.

my_dict = {'a': 10, 'b': 20, 'c': 30}

# Expected Output: c

Exercise 7: Remove a Key-Value Pair from a Dictionary

Write a function to remove a key-value pair from a dictionary given the key.

my_dict = {'a': 1, 'b': 2, 'c': 3}

# Remove the key 'b'
# Expected Output: {'a': 1, 'c': 3}

Exercise 8: Update Dictionary with Another Dictionary

Given two dictionaries, update the first dictionary with the second dictionary. The second dictionary may contain new keys or modify the values of existing keys in the first dictionary.

dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Expected Output: {'a': 1, 'b': 3, 'c': 4}

Exercise 9: Nested Dictionary Search

You have a nested dictionary. Write a function to access the value of a nested key.

nested_dict = {
    'person1': {'name': 'Alice', 'age': 25, 'city': 'New York'},
    'person2': {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}
}

# Access the name of 'person1'
# Expected Output: Alice

Exercise 10: Combine Multiple Dictionaries into One

Given three dictionaries, combine them into a single dictionary. If there are duplicate keys, sum the values for those keys.

dict1 = {'a': 1, 'b': 2}
dict2 = {'a': 3, 'c': 4}
dict3 = {'b': 5, 'd': 6}

# Expected Output: {'a': 4, 'b': 7, 'c': 4, 'd': 6}

Solutions Outline:

  1. Exercise 1: Create a Dictionary from Two Lists
    dict_from_lists = dict(zip(keys, values))
    
  2. Exercise 2: Merge Two Dictionaries
    merged_dict = {**dict1, **dict2}
    
  3. Exercise 3: Check for Key in Dictionary
    'age' in my_dict
    
  4. Exercise 4: Count Occurrences of Items
    from collections import Counter
    count_dict = dict(Counter(my_list))
    
  5. Exercise 5: Invert a Dictionary
    inverted_dict = {v: k for k, v in original_dict.items()}
    
  6. Exercise 6: Find Maximum Value
    max_key = max(my_dict, key=my_dict.get)
    
  7. Exercise 7: Remove a Key-Value Pair
    del my_dict['b']
    
  8. Exercise 8: Update Dictionary
    dict1.update(dict2)
    
  9. Exercise 9: Nested Dictionary Search
    nested_dict['person1']['name']
    
  10. Exercise 10: Combine Multiple Dictionaries
combined_dict = {}
for d in [dict1, dict2, dict3]:
    for k, v in d.items():
        combined_dict[k] = combined_dict.get(k, 0) + v
Leave a Reply 0

Your email address will not be published. Required fields are marked *