Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 24

For loops with dictionaries

Remember our good friend for loops? If we have a list, it’s a good way to do something to each element in the list.

names = ['Brooklyn', 'Manhattan', 'Bronx', 'Staten Island', 'Queens']
for name in names:
  print(name, 'is a borough of New York City')

Print is a common one, but we can also do math.

# These populations are in millions of people
populations = [2.637, 0.474, 1.645, 2.339, 1.455]

# but we want to display the long long long version!
for population in populations:
  print(population * 1000000, "people live... somewhere?")

For loops are specific to lists, but what happens if we try to use them with a dictionary?

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}
for pop in populations:
  print(pop)

Huh, weird, it prints out just the keys. I honestly had to test out what happened as I was writing that code block, because I seriously didn’t remember!

I think it makes more sense to be explicit about what we’re looping through. If we want to loop through the keys, we should ask specifically for the keys.

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}

for city in populations.keys():
  print(city, 'is a city in the world')

This works because even though populations is a dictionary, .keys() is a list. What if I wanted to print out each of the numbers? Yup, I’d just use .values() instead!

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}
for pop in populations.values():
  print(pop, 'is the number of people in the city')

Now this is all good and nice, but the city’s population isn’t very useful without the city’s name, right? How do we loop through both the keys and the values at the same time? With our new friend .items().

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}
for key, value in populations.items():
  print('The city', key, 'has a population of', value)

Ta-da! .keys() will give you the keys, .values() will give you the values, and .items() will give you both at the same time.

The for loop gets a little more exciting, in that you say for key, value and then get to use the key and the value at the same time in the loop.

Weird fact: Even though we typed our dictionary in a certain order - Sanitago, San Diego, Nara, Moscow - dictionaries in Python don’t remember that order. Or rather, older versions of Python don’t remember it, and can mix them all up, but newer versions of Python will remember which ones came first.

If you want specifics: Python 3.6 and earlier versions might forget, but Python 3.7 will remember. It’s probably best to just pretend that

Chapter summary

We learned about looping through dictionaries. Even though loops are normally just for lists, you can use .keys() or .values() to loop through the keys or values of a dictionary. If we’d like to loop through both at the same time, though, we use .items().