Dictionary Exercises
3 min read ·
These exercises are designed to build deep understanding of dictionaries, covering creation, access, updates, looping, nested structures, and tricky behaviors commonly seen in real applications and interviews.
Exercise 1: Create a Dictionary
Create a dictionary to store a person's name, age, and city.
Exercise 2: Access Dictionary Values
Print the value of
name and age.Exercise 3: Safe Access Using get()
Access a key that may not exist.
Provide a default value.
Exercise 4: Add New Key–Value Pair
Add
email to the dictionary.Exercise 5: Update Existing Value
Change the value of
age.Exercise 6: Update Multiple Values
Exercise 7: Remove Item Using pop()
Exercise 8: Remove Last Inserted Item
Exercise 9: Loop Through Dictionary Keys
Exercise 10: Loop Through Values
Exercise 11: Loop Through Key–Value Pairs
Exercise 12: Check If Key Exists
Exercise 13: Count Occurrences Using Dictionary
Count character frequency in a string.
Exercise 14: Create Dictionary from Two Lists
Exercise 15: Nested Dictionary Creation
Exercise 16: Access Nested Dictionary Value
Exercise 17: Add Data to Nested Dictionary
Exercise 18: Loop Through Nested Dictionary
Exercise 19: Copy Dictionary and Observe Behavior
Exercise 20: Use setdefault() for Grouping Data
Advanced Exercise (Conceptual)
Write a program to:
- Convert a list of dictionaries into a single dictionary grouped by a common key
- This pattern is widely used in data processing, analytics, and backend systems
Example idea (text-only):
Group users by city
Group products by category
Group logs by date
Mastering these exercises means you can confidently handle real-world Python data structures, not just academic examples.