Bird
Raised Fist0
Pythonprogramming~20 mins

Dictionary use cases in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested dictionary access
What is the output of this Python code?
Python
data = {'user': {'name': 'Alice', 'age': 30}, 'active': True}
print(data['user']['age'])
A30
B'Alice'
CTrue
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Look inside the nested dictionary under 'user' key.
๐Ÿง  Conceptual
intermediate
1:30remaining
Dictionary keys uniqueness
Which statement about Python dictionary keys is true?
ADictionary keys can be duplicated if values differ.
BDictionary keys must be unique and immutable.
CDictionary keys can be mutable types like lists.
DDictionary keys can be any data type including functions.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens if you try to use a list as a key.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in dictionary update
What error does this code raise?
Python
d = {'a': 1, 'b': 2}
d.update(['c', 3])
ANo error, dictionary updated
BKeyError
CValueError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Check the argument type for update method.
โ“ Predict Output
advanced
1:30remaining
Result of dictionary comprehension with condition
What is the output of this code?
Python
result = {x: x**2 for x in range(5) if x % 2 == 0}
print(result)
A{0: 0, 2: 4, 4: 16}
B{1: 1, 3: 9}
CSyntaxError
D{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Attempts:
2 left
๐Ÿ’ก Hint
Only even numbers are included as keys.
๐Ÿš€ Application
expert
2:30remaining
Count character frequency in string using dictionary
What is the value of 'freq' after running this code?
Python
text = 'banana'
freq = {}
for ch in text:
    freq[ch] = freq.get(ch, 0) + 1
print(freq)
A{'b': 1, 'a': 3, 'n': 2}
B{'b': 1, 'a': 2, 'n': 3}
C{'b': 1, 'a': 3, 'n': 3}
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Count how many times each letter appears in 'banana'.

Practice

(1/5)
1. What is the main purpose of a dictionary in Python?
easy
A. To store data as key-value pairs for quick access
B. To store data in a fixed order like a list
C. To perform mathematical calculations
D. To create a sequence of numbers

Solution

  1. Step 1: Understand dictionary structure

    Dictionaries store data in pairs where each key maps to a value.
  2. Step 2: Identify dictionary use case

    This structure allows fast lookup by key, unlike lists which use indexes.
  3. Final Answer:

    To store data as key-value pairs for quick access -> Option A
  4. Quick Check:

    Dictionaries = key-value pairs [OK]
Hint: Remember: dictionaries use keys to find values fast [OK]
Common Mistakes:
  • Confusing dictionaries with lists or sets
  • Thinking dictionaries keep order like lists
  • Assuming dictionaries store only numbers
2. Which of the following is the correct way to add a new key-value pair to a dictionary my_dict?
easy
A. my_dict.append('key', 'value')
B. my_dict.add('key', 'value')
C. my_dict['key'] = 'value'
D. my_dict.insert('key', 'value')

Solution

  1. Step 1: Recall dictionary syntax for adding items

    To add or update a key-value pair, use square brackets with the key and assign a value.
  2. Step 2: Check each option

    Only my_dict['key'] = 'value' correctly adds or updates the dictionary.
  3. Final Answer:

    my_dict['key'] = 'value' -> Option C
  4. Quick Check:

    Use square brackets to add/update dict items [OK]
Hint: Use square brackets and assignment to add dict items [OK]
Common Mistakes:
  • Using list methods like append or insert on dictionaries
  • Trying to use a non-existent add() method
  • Confusing dictionary syntax with list syntax
3. What will be the output of the following code?
my_dict = {'a': 1, 'b': 2}
my_dict['c'] = 3
print(my_dict)
medium
A. Error: Cannot add new key
B. {'a': 1, 'b': 2}
C. {'c': 3}
D. {'a': 1, 'b': 2, 'c': 3}

Solution

  1. Step 1: Understand dictionary update

    Adding my_dict['c'] = 3 adds a new key 'c' with value 3.
  2. Step 2: Print the updated dictionary

    Printing shows all keys and values including the new one.
  3. Final Answer:

    {'a': 1, 'b': 2, 'c': 3} -> Option D
  4. Quick Check:

    Adding key updates dict content [OK]
Hint: Adding key-value pairs updates dictionary instantly [OK]
Common Mistakes:
  • Expecting original dict without new key
  • Thinking print shows only last added key
  • Assuming error when adding new keys
4. Find the error in this code snippet:
my_dict = {'x': 10, 'y': 20}
print(my_dict['z'])
medium
A. KeyError because 'z' is not in the dictionary
B. SyntaxError due to wrong brackets
C. TypeError because dictionary keys must be integers
D. No error, prints 0

Solution

  1. Step 1: Check dictionary keys

    Keys are 'x' and 'y', but 'z' is not present.
  2. Step 2: Accessing missing key causes error

    Trying to print my_dict['z'] raises a KeyError.
  3. Final Answer:

    KeyError because 'z' is not in the dictionary -> Option A
  4. Quick Check:

    Access missing key = KeyError [OK]
Hint: Accessing missing keys causes KeyError [OK]
Common Mistakes:
  • Assuming missing keys return 0 or None
  • Confusing KeyError with SyntaxError
  • Trying to use wrong bracket types
5. Given a list of tuples representing student names and scores:
students = [('Alice', 85), ('Bob', 90), ('Alice', 95)]

Which code correctly creates a dictionary with the latest score for each student?
hard
A. scores = dict(students) scores['Alice'] = 85
B. scores = {name: score for name, score in students}
C. scores = {} for name, score in students: if name not in scores: scores[name] = score
D. scores = {name: max(score) for name, score in students}

Solution

  1. Step 1: Understand dictionary comprehension behavior

    When keys repeat, the last value overwrites earlier ones.
  2. Step 2: Check each option

    scores = {name: score for name, score in students} uses comprehension that keeps the last score for 'Alice' (95). scores = dict(students) scores['Alice'] = 85 wrongly resets 'Alice' score. scores = {} for name, score in students: if name not in scores: scores[name] = score keeps only first score. scores = {name: max(score) for name, score in students} is invalid syntax.
  3. Final Answer:

    scores = {name: score for name, score in students} -> Option B
  4. Quick Check:

    Dict comprehension overwrites duplicate keys [OK]
Hint: Dict comprehension keeps last value for duplicate keys [OK]
Common Mistakes:
  • Assuming dict keeps first value for duplicates
  • Using invalid syntax for max(score)
  • Manually overwriting values incorrectly