Loops, and where your data lives

INPUT · Slides

Taking them out one at a time

01 / 07

It is the keys that come round

Hand a list to for and the contents came out in turn. Hand it a dictionary and what comes out is — the keys.

Not the values. Everyone is caught out by this once, so say it out loud: the keys are what come round.

prices = {"apple": 120, "orange": 80}for key in prices:    print(key)

Result

apple
orange

02 / 07

Look the value up from the key

Once you have the key, the value is prices[key]. Those two lines are all it takes to go through a whole dictionary.

They come in the order you wrote them. A dictionary remembers the order things went in, so it will not shuffle between runs.

prices = {"apple": 120, "orange": 80}for key in prices:    print(key, prices[key])

Result

apple 120
orange 80

03 / 07

keys gives you the keys

When you want the keys gathered together, use keys().

print it and out comes the unfamiliar dict_keys([...]). That shape is simply how a set of keys is shown, and it loops with for exactly like a list.

for key in prices: and for key in prices.keys(): do the same thing. The shorter one is used more, but writing keys() says plainly that you meant to go through the keys.

prices = {"apple": 120, "orange": 80}print(prices.keys())for key in prices.keys():    print(key)

Result

dict_keys(['apple', 'orange'])
apple
orange

04 / 07

values gives you the values

When only the values matter, use values(). It suits anything where the key is beside the point — working out a total, say.

print this one and you get dict_values([...]). The contents come in the order you wrote them.

prices = {"apple": 120, "orange": 80}print(prices.values())total = 0for value in prices.values():    total += valueprint(total)

Result

dict_values([120, 80])
200

05 / 07

items gives you the pairs

When you want both the key and the value, use items().

print it as it is and out come bracketed pairs like ('apple', 120). What that "two things joined together" really is turns out to be the tuple, the last lesson in this group. For now, read it as keys and values lined up in pairs.

prices = {"apple": 120, "orange": 80}print(prices.items())

Result

dict_items([('apple', 120), ('orange', 80)])

06 / 07

Taking both at once

This is where items() earns its keep. You can give for two names, separated by a comma.

Write for key, value in prices.items(): and each time round, key holds the key and value holds the value. There is no need to write prices[key], which is why this is the version you see most.

prices = {"apple": 120, "orange": 80}for key, value in prices.items():    print(key, value)

Result

apple 120
orange 80

07 / 07

Take only what you need

Let us line the ways of looping over a dictionary up.

  • for key in person: … keys only. The shortest
  • for value in person.values(): … values only
  • for key, value in person.items(): … both

Taking only what you need is the clean way to write it. Looping over the keys just to look the values back up, when the keys go unused, is a slight detour.

Next it is time to put dictionaries and lists together. Everything so far joins up.

person = {"name": "Yui", "age": 15}for key, value in person.items():    print(f"{key} is {value}")

Result

name is Yui
age is 15