Python - Iterating Over Dicts
Code Life
By Pierre Marsaa
Introduction to Dictionary Iteration
Now that we know how to create, access, and modify dictionaries, let's learn how to loop through them! There are three main ways to iterate through a dictionary in Python.
When you use a for loop with a dictionary directly, Python iterates over the keys by default:
↗️
Loading editor...
↶
↷
Notice that the loop gives us only the keys, not the values. To access the values, we need to use the key inside the loop.
↗️
Loading editor...
↶
↷
Verify