Python Dictionary Exercises

Welcome to The Coding College, your go-to platform for learning Python through practical examples and exercises! In this post, we’ll provide hands-on exercises to help you master Python dictionaries, one of the most versatile data structures in Python.

These exercises are perfect for beginners and intermediates looking to strengthen their understanding of dictionaries.

Why Practice Python Dictionaries?

Dictionaries are powerful because they allow you to store data in key-value pairs, making it easier to manage and access information. By practicing, you’ll be able to:

  • Efficiently retrieve and modify data.
  • Solve real-world problems involving structured data.
  • Write cleaner, more efficient code.

Python Dictionary Exercises

Exercise 1: Create a Dictionary

Create a dictionary named student that contains the following key-value pairs:

  • "name": "Alice"
  • "age": 25
  • "major": "Computer Science"

Expected Output:

{'name': 'Alice', 'age': 25, 'major': 'Computer Science'}  

Exercise 2: Access Dictionary Items

Using the student dictionary from Exercise 1, retrieve the value associated with the key "major".

Hint: Use square brackets or the get() method.

Exercise 3: Add a New Key-Value Pair

Add a new key-value pair to the student dictionary:

  • "graduation_year": 2024

Expected Output:

{'name': 'Alice', 'age': 25, 'major': 'Computer Science', 'graduation_year': 2024}  

Exercise 4: Modify a Value

Update the "age" key in the student dictionary to 26.

Expected Output:

{'name': 'Alice', 'age': 26, 'major': 'Computer Science', 'graduation_year': 2024}  

Exercise 5: Remove a Key-Value Pair

Remove the "major" key from the student dictionary using the pop() method.

Expected Output:

{'name': 'Alice', 'age': 26, 'graduation_year': 2024}  

Exercise 6: Merge Dictionaries

Given two dictionaries:

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

Merge dict2 into dict1 using the update() method.

Expected Output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}  

Exercise 7: Loop Through a Dictionary

Given the dictionary:

person = {"name": "Bob", "age": 30, "city": "New York"}  

Write a loop to print each key and its corresponding value.

Expected Output:

name: Bob  
age: 30  
city: New York  

Exercise 8: Nested Dictionary

Create a nested dictionary to represent students and their details:

students = {  
    "student1": {"name": "Alice", "age": 25},  
    "student2": {"name": "Bob", "age": 22}  
}  

Access the "age" of "student1".

Expected Output:

25  

Exercise 9: Dictionary Comprehension

Write a dictionary comprehension to create a dictionary where the keys are numbers from 1 to 5, and the values are their squares.

Expected Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}  

Exercise 10: Check for a Key

Given the dictionary:

inventory = {"apples": 10, "bananas": 5, "oranges": 7}  

Check if the key "bananas" exists in the dictionary. If it does, print "Bananas are available!".

Expected Output:

Bananas are available!  

Exercise 11: Count Occurrences Using a Dictionary

Write a program to count the occurrences of each character in the string "hello world".

Expected Output:

{'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}  

Exercise 12: Remove Duplicates from a Dictionary

Given the dictionary:

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

Write a program to remove key-value pairs with duplicate values.

Expected Output:

{'b': 2, 'd': 3}  

Why Practice with The Coding College?

At The Coding College, we’re committed to providing hands-on learning opportunities that build confidence and expertise. By completing these exercises, you’ll solidify your Python dictionary skills and be prepared to tackle more advanced programming challenges.

Conclusion

Dictionaries are an indispensable part of Python programming. Regular practice with real-world scenarios, like the ones above, ensures you’ll be able to use them effectively in your projects.

Leave a Comment