Dictionaries are iterable mappings, meaning you can iterate over them similar to other collections, such as a string, a tuple, a list, or a set. However, because dictionaries consist of keys and values, you'll need to tackle iteration a bit differently:
users = {'mary': 22, 'caroline': 99, 'harry': 20}
for user, age in users.items():
print(user, age)
The dict.items() method gives you a list of tuple objects you can iterate over.
While there's also another way of accessing items in a dictionary, the pattern shown in the code snippet above is the most straightforward to read. It clarifies that you're iterating over both the keys and values of the dictionary.
But what if you want to only access the keys or the values?
Access Only Keys or Only Values
You can also iterate over only the keys or only the values in a dictionary. In fact, if you directly iterate over a dictionary without calling a method, such as .items() on it, Python will iterate over the dictionary's keys:
users = {'mary': 22, 'caroline': 99, 'harry': 20}
for k in users:
print(k)
# OUTPUT:
# mary
# caroline
# harry
Alternatively, you can use the distinct dictionary method .keys() to iterate over just the keys:
users = {'mary': 22, 'caroline': 99, 'harry': 20}
for k in users.keys():
print(k)
# OUTPUT:
# mary
# caroline
# harry
And if you need to access all values in a dictionary, you'll need to use .values():
users = {'mary': 22, 'caroline': 99, 'harry': 20}
for v in users.values():
print(v)
# OUTPUT:
# 22
# 99
# 20
As you can see, dict.keys() allows you to iterate over only the keys of your dictionary, while dict.values() allows you to access all the values.
Playground: Iterating Practice
- Change the age of
"caroline"from99to26. - Use the code playground below to age each user for 10 years.
- Play around with the code and add or delete some dictionary entries. How does that affect your output?
# your turn!
Explore More Dictionary Methods
Just like with other data types, there are a lot of methods you can use on dictionaries to do something with them. You invoke these methods by calling them on the dictionary object. For example, this is how you call the .clear() method on your dictionary:
users = {'mary': 22, 'caroline': 99, 'harry': 20}
users.clear()
# OUTPUT: {}
The dict.clear() method empties your dictionary by completely removing all key-value pairs.
Head over to the documentation on Python dictionaries and explore some of the dictionary methods. Try to implement some of them on your example dictionary, e.g.:
What can you accomplish when using these methods? Remember to keep the documentation handy and practice reading it. It'll feel complicated and unfamiliar at the beginning, but learning to be comfortable with reading documentation is a vital skill for becoming a developer. So keep coming back to it, and don't let yourself get too frustrated if you don't understand everything right from the start.
Additional Resources
- Official Python Tutorial: Dictionaries
- Python Documentation: Dictionaries
- Programiz: Dictionary Methods with examples
Summary: Iterate Python Dictionary
Dictionaries are iterable mappings of key-value pairs. If you directly iterate over a dict, you're iterating over its keys. It's good to be explicit about what you want to iterate over by using the appropriate method:
dict.keys()iterates over the keys of your dictionary.dict.values()iterates over the values of your dictionary.dict.items()iterates over the both the keys and values of your dictionary, packaged in a tuple of the shape(key, value).
Like other data types, dictionaries also come with predefined methods that allow you to perform common actions on your dictionaries. You can learn more about them in the dictionary documentation.
In the next lesson, you'll recap all the new data types you've gotten to know before applying them to your projects.