Dictionaries store data in pairs: keys and values. You often need to get just the keys, just the values, or both together to work with the data.
Dictionary keys, values, and items in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
dict.keys() dict.values() dict.items()
dict.keys() returns all the keys in the dictionary.
dict.values() returns all the values in the dictionary.
dict.items() returns pairs of (key, value) as tuples.
Examples
Python
my_dict = {'apple': 3, 'banana': 5}
keys = my_dict.keys()
print(keys)Python
my_dict = {'apple': 3, 'banana': 5}
values = my_dict.values()
print(values)Python
my_dict = {'apple': 3, 'banana': 5}
items = my_dict.items()
print(items)Sample Program
This program creates a dictionary of fruits with quantities. It prints all keys, values, and key-value pairs.
Python
fruits = {'apple': 10, 'banana': 20, 'cherry': 30}
print('Keys:', list(fruits.keys()))
print('Values:', list(fruits.values()))
print('Items:', list(fruits.items()))Important Notes
The keys(), values(), and items() methods return special views, so converting them to a list shows them clearly.
You can loop directly over these views in a for-loop without converting.
Summary
Dictionaries store data as key-value pairs.
Use keys() to get all keys, values() for all values, and items() for all pairs.
These methods help you access and work with dictionary data easily.
Practice
1. Which method would you use to get all the keys from a Python dictionary
my_dict?easy
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]
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
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]
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
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]
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
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]
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
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]
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
