Bird
Raised Fist0
Pythonprogramming~20 mins

Dictionary keys, values, and items 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 dictionary keys() method
What is the output of this code snippet?
Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(list(my_dict.keys()))
A['a', 'b', 'c']
B['1', '2', '3']
C['a', 'b', 'c', 'd']
Ddict_keys(['a', 'b', 'c'])
Attempts:
2 left
๐Ÿ’ก Hint
The keys() method returns a view of the dictionary's keys. Converting it to a list shows the keys as a list.
โ“ Predict Output
intermediate
2:00remaining
Output of dictionary values() method
What will this code print?
Python
data = {'x': 10, 'y': 20}
vals = data.values()
print(sum(vals))
A30
B['10', '20']
CTypeError
Ddict_values([10, 20])
Attempts:
2 left
๐Ÿ’ก Hint
The values() method returns a view of values which can be summed directly.
โ“ Predict Output
advanced
2:00remaining
Output of iterating dictionary items
What is the output of this code?
Python
d = {'one': 1, 'two': 2}
for k, v in d.items():
    print(f"{k}:{v}")
A1\n2
B('one', 1)\n('two', 2)
Cone\ntwo
Done:1\ntwo:2
Attempts:
2 left
๐Ÿ’ก Hint
items() returns key-value pairs which are unpacked in the loop.
โ“ Predict Output
advanced
2:00remaining
Length of dictionary keys view
What does this code print?
Python
my_dict = {1: 'a', 2: 'b', 3: 'c'}
keys_view = my_dict.keys()
print(len(keys_view))
ATypeError
B3
Cdict_keys([1, 2, 3])
D0
Attempts:
2 left
๐Ÿ’ก Hint
The keys view supports len() to get the number of keys.
โ“ Predict Output
expert
2:00remaining
Output of modifying dictionary during iteration
What happens when this code runs?
Python
d = {'a': 1, 'b': 2, 'c': 3}
for k in d.keys():
    if k == 'b':
        d.pop(k)
print(d)
A{'a': 1, 'c': 3}
B{'a': 1, 'b': 2, 'c': 3}
CRuntimeError: dictionary changed size during iteration
D{'a': 1, 'b': 2}
Attempts:
2 left
๐Ÿ’ก Hint
Modifying a dictionary while iterating over its keys causes an error.

Practice

(1/5)
1. Which method would you use to get all the keys from a Python dictionary my_dict?
easy
A. my_dict.values()
B. my_dict.get()
C. my_dict.items()
D. my_dict.keys()

Solution

  1. Step 1: Understand dictionary methods

    The keys() method returns all keys in the dictionary.
  2. Step 2: Match method to requirement

    Since we want all keys, my_dict.keys() is the correct method.
  3. Final Answer:

    my_dict.keys() -> Option D
  4. Quick Check:

    keys() = my_dict.keys() [OK]
Hint: Keys come from keys(), values from values(), pairs from items() [OK]
Common Mistakes:
  • Confusing keys() with values()
  • Using get() which retrieves a single value
  • Using items() which returns key-value pairs
2. Which of the following is the correct syntax to get all values from a dictionary data?
easy
A. data.values()
B. data.get_values()
C. data.values
D. values(data)

Solution

  1. Step 1: Recall method syntax

    Dictionary methods require parentheses to call them, so values() is correct.
  2. Step 2: Check each option

    data.values misses parentheses, get_values() and values(data) are invalid.
  3. Final Answer:

    data.values() -> Option A
  4. Quick Check:

    values() needs parentheses [OK]
Hint: Always add () to call dictionary methods like values() [OK]
Common Mistakes:
  • Forgetting parentheses after method name
  • Using non-existent methods like get_values()
  • Trying to call values() as a function with dictionary argument
3. What is the output of this code?
my_dict = {'a': 1, 'b': 2}
print(list(my_dict.items()))
medium
A. [('a', 1), ('b', 2)]
B. ['a', 'b']
C. [1, 2]
D. Error

Solution

  1. Step 1: Understand items() method

    The items() method returns key-value pairs as tuples.
  2. Step 2: Convert items to list

    Using list() converts these pairs into a list of tuples: [('a', 1), ('b', 2)].
  3. Final Answer:

    [('a', 1), ('b', 2)] -> Option A
  4. Quick Check:

    items() = list of (key, value) pairs [OK]
Hint: items() returns pairs; list() shows them as list of tuples [OK]
Common Mistakes:
  • Thinking items() returns only keys or only values
  • Expecting a dictionary instead of list of tuples
  • Confusing items() with keys() or values()
4. Find the error in this code snippet:
my_dict = {'x': 10, 'y': 20}
for key, value in my_dict.keys():
    print(key, value)
medium
A. Syntax error in for loop
B. keys() returns only keys, cannot unpack into two variables
C. Missing parentheses after print
D. No error, code runs fine

Solution

  1. Step 1: Check what keys() returns

    keys() returns only keys, so each item is a single value, not a pair.
  2. Step 2: Understand unpacking in for loop

    The loop tries to unpack each key into two variables, causing an error.
  3. Final Answer:

    keys() returns only keys, cannot unpack into two variables -> Option B
  4. Quick Check:

    keys() = keys only, no pairs [OK]
Hint: keys() gives one value per item; items() gives pairs [OK]
Common Mistakes:
  • Trying to unpack keys() into two variables
  • Confusing keys() with items()
  • Assuming keys() returns key-value pairs
5. You have a dictionary grades = {'Alice': 85, 'Bob': 92, 'Charlie': 78}. Which code snippet correctly prints each student's name and grade using dictionary methods?
hard
A. for name, grade in grades.values(): print(name, grade)
B. for name, grade in grades.keys(): print(name, grade)
C. for name, grade in grades.items(): print(name, grade)
D. for grade in grades.values(): print(grade)

Solution

  1. Step 1: Identify method to get pairs

    items() returns key-value pairs, perfect for name and grade.
  2. Step 2: Check each option

    for name, grade in grades.values(): print(name, grade) unpacks grades.values() (single values): error. for name, grade in grades.keys(): print(name, grade) unpacks grades.keys() (single keys): error. for grade in grades.values(): print(grade) prints only grades. for name, grade in grades.items(): print(name, grade) correctly unpacks grades.items() pairs.
  3. Final Answer:

    for name, grade in grades.items(): print(name, grade) -> Option C
  4. Quick Check:

    items() gives key-value pairs for easy unpacking [OK]
Hint: Use items() to loop over keys and values together [OK]
Common Mistakes:
  • Using values() when keys and values needed
  • Unpacking values() which are single values
  • Using keys() without accessing values