Dictionary keys, values, and items in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use dictionary methods like keys(), values(), and items(), it is important to know how the time to get these collections changes as the dictionary grows.
We want to understand how long it takes to access these parts of a dictionary as it gets bigger.
Analyze the time complexity of the following code snippet.
my_dict = {i: i*2 for i in range(n)}
keys_list = list(my_dict.keys())
values_list = list(my_dict.values())
items_list = list(my_dict.items())
This code creates a dictionary with n items, then makes lists of its keys, values, and key-value pairs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over all dictionary entries to collect keys, values, or items.
- How many times: Once for each element in the dictionary, so n times.
As the dictionary size grows, the time to get keys, values, or items grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The work grows in a straight line with the number of items.
Time Complexity: O(n)
This means the time to get all keys, values, or items grows directly with the number of entries in the dictionary.
[X] Wrong: "Getting keys, values, or items is instant no matter the dictionary size."
[OK] Correct: Although the dictionary stores data efficiently, collecting all keys or values requires looking at each entry once, so it takes longer as the dictionary grows.
Understanding how dictionary methods scale helps you explain your code's efficiency clearly and shows you know how data size affects performance.
What if we only accessed a single key or value instead of all keys or values? How would the time complexity change?
Practice
my_dict?Solution
Step 1: Understand dictionary methods
Thekeys()method returns all keys in the dictionary.Step 2: Match method to requirement
Since we want all keys,my_dict.keys()is the correct method.Final Answer:
my_dict.keys() -> Option DQuick Check:
keys() = my_dict.keys() [OK]
- Confusing keys() with values()
- Using get() which retrieves a single value
- Using items() which returns key-value pairs
data?Solution
Step 1: Recall method syntax
Dictionary methods require parentheses to call them, sovalues()is correct.Step 2: Check each option
data.valuesmisses parentheses,get_values()andvalues(data)are invalid.Final Answer:
data.values() -> Option AQuick Check:
values() needs parentheses [OK]
- Forgetting parentheses after method name
- Using non-existent methods like get_values()
- Trying to call values() as a function with dictionary argument
my_dict = {'a': 1, 'b': 2}
print(list(my_dict.items()))Solution
Step 1: Understand items() method
Theitems()method returns key-value pairs as tuples.Step 2: Convert items to list
Usinglist()converts these pairs into a list of tuples: [('a', 1), ('b', 2)].Final Answer:
[('a', 1), ('b', 2)] -> Option AQuick Check:
items() = list of (key, value) pairs [OK]
- Thinking items() returns only keys or only values
- Expecting a dictionary instead of list of tuples
- Confusing items() with keys() or values()
my_dict = {'x': 10, 'y': 20}
for key, value in my_dict.keys():
print(key, value)Solution
Step 1: Check what keys() returns
keys()returns only keys, so each item is a single value, not a pair.Step 2: Understand unpacking in for loop
The loop tries to unpack each key into two variables, causing an error.Final Answer:
keys() returns only keys, cannot unpack into two variables -> Option BQuick Check:
keys() = keys only, no pairs [OK]
- Trying to unpack keys() into two variables
- Confusing keys() with items()
- Assuming keys() returns key-value pairs
grades = {'Alice': 85, 'Bob': 92, 'Charlie': 78}. Which code snippet correctly prints each student's name and grade using dictionary methods?Solution
Step 1: Identify method to get pairs
items()returns key-value pairs, perfect for name and grade.Step 2: Check each option
for name, grade in grades.values(): print(name, grade) unpacksgrades.values()(single values): error. for name, grade in grades.keys(): print(name, grade) unpacksgrades.keys()(single keys): error. for grade in grades.values(): print(grade) prints only grades. for name, grade in grades.items(): print(name, grade) correctly unpacksgrades.items()pairs.Final Answer:
for name, grade in grades.items(): print(name, grade) -> Option CQuick Check:
items() gives key-value pairs for easy unpacking [OK]
- Using values() when keys and values needed
- Unpacking values() which are single values
- Using keys() without accessing values
