Practicing dictionary exercises is one of the best ways to improve your Python skills. Dictionaries are widely used in real-world applications such as data analysis, APIs, databases, and web development.
In this tutorial, you will find Python dictionary exercises with solutions from beginner to intermediate level.
Exercise 1: Create a Dictionary
Task:
Create a dictionary with the following data:
- name = John
- age = 25
- city = London
Solution:
person = {
"name": "John",
"age": 25,
"city": "London"
}
print(person)Exercise 2: Access Dictionary Value
Task:
Print the value of "age".
Solution:
person = {
"name": "John",
"age": 25,
"city": "London"
}
print(person["age"])Output:
25Exercise 3: Add New Item
Task:
Add "job": "Developer" to the dictionary.
Solution:
person["job"] = "Developer"
print(person)Exercise 4: Update Value
Task:
Change "city" to "Paris".
Solution:
person["city"] = "Paris"
print(person)Exercise 5: Remove an Item
Task:
Remove "age" from the dictionary.
Solution:
person.pop("age")
print(person)Exercise 6: Check Key Exists
Task:
Check if "name" exists in the dictionary.
Solution:
if "name" in person:
print("Key exists")Output:
Key existsExercise 7: Loop Through Keys
Task:
Print all keys in the dictionary.
Solution:
for key in person:
print(key)Exercise 8: Loop Through Values
Task:
Print all values in the dictionary.
Solution:
for value in person.values():
print(value)Exercise 9: Loop Through Key-Value Pairs
Task:
Print all keys and values.
Solution:
for key, value in person.items():
print(key, ":", value)Exercise 10: Merge Two Dictionaries
Task:
Merge these two dictionaries:
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}Solution:
dict1.update(dict2)
print(dict1)Exercise 11: Nested Dictionary Access
Task:
Print Alice’s age.
students = {
"student1": {"name": "John", "age": 20},
"student2": {"name": "Alice", "age": 22}
}Solution:
print(students["student2"]["age"])Exercise 12: Count Dictionary Items
Task:
Find number of items in dictionary.
Solution:
print(len(person))Exercise 13: Copy Dictionary
Task:
Create a copy of dictionary.
Solution:
new_person = person.copy()
print(new_person)Exercise 14: Get Value Safely
Task:
Get "salary" safely without error.
Solution:
print(person.get("salary", "Not Found"))Exercise 15: Create Dictionary from Keys
Task:
Create dictionary from keys list.
Solution:
keys = ["name", "age", "city"]
new_dict = dict.fromkeys(keys, "Unknown")
print(new_dict)Real-World Practice: Inventory System
Task:
Create a system for products and quantities.
Solution:
inventory = {
"Laptop": 10,
"Mouse": 25,
"Keyboard": 15
}
inventory["Monitor"] = 8
inventory.pop("Mouse")
for product, qty in inventory.items():
print(product, qty)Real-World Practice: User Management
Task:
Manage user profiles.
users = {
"user1": {"name": "John", "role": "admin"},
"user2": {"name": "Alice", "role": "editor"}
}
users["user3"] = {"name": "David", "role": "viewer"}
for user, info in users.items():
print(user, info["name"], info["role"])Common Mistakes
Mistake 1: Using wrong key
print(person["country"]) # Error if key does not exist✔ Use:
print(person.get("country"))Mistake 2: Forgetting items() in loop
❌ Wrong:
for key, value in person:
print(key, value)✔ Correct:
for key, value in person.items():
print(key, value)Conclusion
Practicing dictionary exercises helps you master one of the most important Python data structures.
You learned:
- Creating dictionaries
- Accessing and updating data
- Looping through dictionaries
- Using built-in methods
- Working with nested dictionaries
Keep practicing these exercises to improve your Python programming skills for real-world applications like APIs, databases, and backend development.


0 Comments