Python · Python Collections

Dictionaries — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1Key-Value PairsEasy

Create student = {"name": "Asha", "age": 22} and print it along with its type().

Q2Accessing ValuesEasy

Using student = {"name": "Asha", "age": 22}, print the value for the key "name".

Q3Adding / UpdatingEasy

Using student = {"name": "Asha"}, add a new key "age" with value 22, then update "name" to "Asha Sharma".

Q4len()Easy

Print the number of key-value pairs in person = {"name": "Bob", "age": 30, "city": "Delhi"}.

Q5keys() / values()Easy

Using person = {"name": "Bob", "age": 30}, print its keys and its values separately.

Q6items()Easy

Using person = {"name": "Bob", "age": 30}, print each key-value pair using .items().

Q7MembershipEasy

Using person = {"name": "Bob", "age": 30}, print whether "name" and "email" are each in the dictionary.

Q8IterationEasy

Using person = {"name": "Bob", "age": 30, "city": "Delhi"}, loop through it and print each key on its own line.

Q9IterationEasy

Using person = {"name": "Bob", "age": 30}, loop through it and print each key: value pair using .items() and unpacking.

Q10delEasy

Using person = {"name": "Bob", "age": 30, "city": "Delhi"}, delete the "city" key using del, then print the dictionary.

Q11get()Medium

Using person = {"name": "Bob"}, use .get() to safely read "age" with a default of "Unknown", avoiding a KeyError.

Q12update()Medium

Given person = {"name": "Bob", "age": 30} and updates = {"age": 31, "city": "Delhi"}, use .update() to merge them, then print the result.

Q13pop()Medium

Given person = {"name": "Bob", "age": 30, "city": "Delhi"}, remove and print the value for "age" using .pop(), then print the remaining dictionary.

Q14pop() with DefaultMedium

Given person = {"name": "Bob"}, use .pop() to try removing "email" with a default of None so it doesn't raise a KeyError.

Q15Nested DictionariesMedium

Create student = {"name": "Asha", "grades": {"math": 90, "science": 85}}. Print the science grade.

Q16Building DictsMedium

Build a dictionary mapping numbers 1 to 5 to their squares, using a loop (no comprehensions).

Q17Dict from ListsMedium

Given keys = ["name", "age", "city"] and values = ["Asha", 22, "Pune"], build a dictionary using zip() and dict().

Q18setdefault()Medium

Given counts = {"a": 1}, use .setdefault() to add "b" with a default of 0 only if it doesn't already exist, then increment it.

Q19Frequency CountingMedium

Given word = "mississippi", build a dictionary counting how many times each letter appears, using a loop.

Q20FormattingMedium

Given prices = {"Pen": 10.5, "Notebook": 45.0, "Eraser": 5.75}, print each item and price in an aligned table using .items().

Q21Real-WorldMedium

Ask the user for a sentence and build a dictionary counting how many times each word appears, using .split() and .get().

Q22Real-WorldMedium

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).

Q23Real-WorldMedium

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).

Q24GroupingMedium

Given students = [("Alice", "A"), ("Bob", "B"), ("Carol", "A"), ("David", "B")], group student names by their grade into a dictionary of lists.

Q25Real-WorldMedium

Given two dictionaries menu_prices = {"Pizza": 250, "Burger": 120} and an order order = {"Pizza": 2, "Burger": 1}, compute and print the total bill.

Q26MergingMedium

Given defaults = {"theme": "light", "font_size": 12} and user_settings = {"font_size": 14}, merge them using the ** unpacking syntax so user_settings overrides defaults.

Q27Real-WorldMedium

Given attendance = {"Alice": True, "Bob": False, "Carol": True}, print the names of everyone marked present.

Q28Character CountingMedium

Given text = "hello world", count how many times each character appears (including spaces), and print the result.

Q29DebuggingMedium

The following code crashes with a KeyError. Find and fix the bug.

scores = {"Alice": 90, "Bob": 85}
print(scores["Carol"])
Q30Dict vs ListMedium

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.

Q31Nested DictionariesHard

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.

Q32SortingHard

Given scores = {"Alice": 88, "Bob": 95, "Carol": 79}, print the names sorted by score, highest first, using sorted() with a lambda key on .items().

Q33Finding ExtremesHard

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).

Q34Inverting a DictHard

Given country_codes = {"IN": "India", "US": "United States", "FR": "France"}, build an inverted dictionary mapping full names back to codes.

Q35GroupingHard

Given words = ["apple", "banana", "avocado", "blueberry", "cherry", "clementine"], group them into a dictionary keyed by their first letter.

Q36Nested DictionariesHard

Given students = {"Alice": {"math": 90, "science": 85}, "Bob": {"math": 70, "science": 75}}, compute and print each student's average score.

Q37Comparing DictsHard

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.

Q38Real-WorldHard

Given transactions = [("deposit", 500), ("withdraw", 200), ("deposit", 300), ("withdraw", 100)], build a dictionary summarizing the total deposited and total withdrawn.

Q39DebuggingHard

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)
Q40ApplicationsHard

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.

Q41Mini-ProjectMini-Project

Build a "Contact Book" using a dictionary of dictionaries ({name: {"phone": ..., "email": ...}}), with a menu loop supporting add, search, delete, and exit.

Q42Mini-ProjectMini-Project

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.

Q43Mini-ProjectMini-Project

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.

Q44Mini-ProjectMini-Project

Build a "Word Frequency Analyzer". Ask for a paragraph, then print the top 3 most frequent words and their counts.

Q45Mini-ProjectMini-Project

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.

Q46InterviewInterview

Explain why dictionary keys must be hashable (tying back to sets and tuples), demonstrating a working tuple key and a failing list key.

Q47InterviewInterview

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.

Q48InterviewInterview

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.

Q49InterviewInterview

Demonstrate the shallow-copy pitfall with nested dictionaries: copying with .copy() still shares inner dicts, and show the copy.deepcopy() fix.

Q50CapstoneInterview

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