Dictionaries — Practice Questions
50 questions. Try each one yourself before checking the answer.
Create student = {"name": "Asha", "age": 22} and print it along with its type().
Using student = {"name": "Asha", "age": 22}, print the value for the key "name".
Using student = {"name": "Asha"}, add a new key "age" with value 22, then update "name" to "Asha Sharma".
Print the number of key-value pairs in person = {"name": "Bob", "age": 30, "city": "Delhi"}.
Using person = {"name": "Bob", "age": 30}, print its keys and its values separately.
Using person = {"name": "Bob", "age": 30}, print each key-value pair using .items().
Using person = {"name": "Bob", "age": 30}, print whether "name" and "email" are each in the dictionary.
Using person = {"name": "Bob", "age": 30, "city": "Delhi"}, loop through it and print each key on its own line.
Using person = {"name": "Bob", "age": 30}, loop through it and print each key: value pair using .items() and unpacking.
Using person = {"name": "Bob", "age": 30, "city": "Delhi"}, delete the "city" key using del, then print the dictionary.
Using person = {"name": "Bob"}, use .get() to safely read "age" with a default of "Unknown", avoiding a KeyError.
Given person = {"name": "Bob", "age": 30} and updates = {"age": 31, "city": "Delhi"}, use .update() to merge them, then print the result.
Given person = {"name": "Bob", "age": 30, "city": "Delhi"}, remove and print the value for "age" using .pop(), then print the remaining dictionary.
Given person = {"name": "Bob"}, use .pop() to try removing "email" with a default of None so it doesn't raise a KeyError.
Create student = {"name": "Asha", "grades": {"math": 90, "science": 85}}. Print the science grade.
Build a dictionary mapping numbers 1 to 5 to their squares, using a loop (no comprehensions).
Given keys = ["name", "age", "city"] and values = ["Asha", 22, "Pune"], build a dictionary using zip() and dict().
Given counts = {"a": 1}, use .setdefault() to add "b" with a default of 0 only if it doesn't already exist, then increment it.
Given word = "mississippi", build a dictionary counting how many times each letter appears, using a loop.
Given prices = {"Pen": 10.5, "Notebook": 45.0, "Eraser": 5.75}, print each item and price in an aligned table using .items().
Ask the user for a sentence and build a dictionary counting how many times each word appears, using .split() and .get().
Build a simple "Phonebook" dictionary and write code to look up a name, printing "Not found" if the name doesn't exist (use .get(), no exceptions).
Build an "Inventory" dictionary mapping item names to quantities. Ask the user for an item and an amount to add to its stock (creating the item if it doesn't exist).
Given students = [("Alice", "A"), ("Bob", "B"), ("Carol", "A"), ("David", "B")], group student names by their grade into a dictionary of lists.
Given two dictionaries menu_prices = {"Pizza": 250, "Burger": 120} and an order order = {"Pizza": 2, "Burger": 1}, compute and print the total bill.
Given defaults = {"theme": "light", "font_size": 12} and user_settings = {"font_size": 14}, merge them using the ** unpacking syntax so user_settings overrides defaults.
Given attendance = {"Alice": True, "Bob": False, "Carol": True}, print the names of everyone marked present.
Given text = "hello world", count how many times each character appears (including spaces), and print the result.
The following code crashes with a KeyError. Find and fix the bug.
scores = {"Alice": 90, "Bob": 85}
print(scores["Carol"])Given a list of (id, name) tuples, convert it into a dictionary keyed by id for fast lookup, and demonstrate looking up one ID directly instead of searching the list.
Given company = {"engineering": {"headcount": 50}, "sales": {"headcount": 30}}, update engineering's headcount to 55, then add a brand-new department "marketing" with its own nested dict.
Given scores = {"Alice": 88, "Bob": 95, "Carol": 79}, print the names sorted by score, highest first, using sorted() with a lambda key on .items().
Given prices = {"Pen": 10.5, "Notebook": 45.0, "Eraser": 5.75}, find the item with the HIGHEST price without using max() directly on .items() (use a manual loop instead).
Given country_codes = {"IN": "India", "US": "United States", "FR": "France"}, build an inverted dictionary mapping full names back to codes.
Given words = ["apple", "banana", "avocado", "blueberry", "cherry", "clementine"], group them into a dictionary keyed by their first letter.
Given students = {"Alice": {"math": 90, "science": 85}, "Bob": {"math": 70, "science": 75}}, compute and print each student's average score.
Given dict1 = {"a": 1, "b": 2} and dict2 = {"b": 2, "a": 1}, print whether they're equal with ==, and explain in a comment why key order doesn't matter for equality.
Given transactions = [("deposit", 500), ("withdraw", 200), ("deposit", 300), ("withdraw", 100)], build a dictionary summarizing the total deposited and total withdrawn.
The following frequency counter always crashes on the first NEW word. Find and fix the bug.
text = "the cat sat on the mat"
word_count = {}
for word in text.split():
word_count[word] += 1
print(word_count)Given text = "the quick brown fox jumps over the lazy dog the fox runs", find and print the single most frequent word and its count, using a dictionary and a manual max-tracking loop.
Build a "Contact Book" using a dictionary of dictionaries ({name: {"phone": ..., "email": ...}}), with a menu loop supporting add, search, delete, and exit.
Build an "Inventory Management System" using a dictionary mapping item names to {"quantity": int, "price": float}. Support adding stock and printing a full valued report.
Build a "Grade Book System" using a dictionary mapping student names to a list of scores. Support adding a score and printing each student's average.
Build a "Word Frequency Analyzer". Ask for a paragraph, then print the top 3 most frequent words and their counts.
Build a "Shopping Cart Calculator" using a menu dict of prices and a cart dict of item->quantity. Print an itemized receipt and grand total.
Explain why dictionary keys must be hashable (tying back to sets and tuples), demonstrating a working tuple key and a failing list key.
Explain why, since Python 3.7, dictionaries preserve insertion order, and demonstrate that iterating a dict returns keys in the order they were first added — not sorted or random.
Explain when a dictionary is the better choice over a list of tuples for looking up records, and demonstrate the performance-relevant difference with a comment.
Demonstrate the shallow-copy pitfall with nested dictionaries: copying with .copy() still shares inner dicts, and show the copy.deepcopy() fix.
Build a "Dictionary Mastery Report" — the most complete demonstration in this module. Given a sentence from the user, build a word-frequency dictionary, then print: total distinct words, the most frequent word, words sorted alphabetically with counts, and words that appear only once — all in a labeled report.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session