Loops, and where your data lives

INPUT · Slides

Lining dictionaries up in a list

01 / 07

One person fitted in a dictionary

With a dictionary, one person worth of data comes out neatly. The name and the score are one thing.

But a register does not stop at one person. What do you do when you want many of them?

person = {"name": "Yui", "score": 80}print(person["name"], person["score"])

Result

Yui 80

02 / 07

Put the dictionaries in a list

The answer is to line the dictionaries up inside a list. Between [ and ] you write dictionaries separated by commas.

Read it like this: the list is the whole register, each dictionary is one person. Hold on to that sentence and the rest is only tools you already have.

people = [{"name": "Yui", "score": 80}, {"name": "Sou", "score": 55}]print(people[0])

Result

{'name': 'Yui', 'score': 80}

03 / 07

Take it out in two steps

people[0] was a dictionary. A dictionary can be looked up by key, so you can carry straight on with ["name"].

Read people[0]["name"] from the left.

  • people[0] … the first record in the register (a dictionary)
  • ["name"] … the name in that dictionary

Trying to take it in at once is confusing, so the knack is to read one set of brackets at a time.

people = [{"name": "Yui", "score": 80}, {"name": "Sou", "score": 55}]print(people[0]["name"])print(people[1]["score"])

Result

Yui
55

04 / 07

Loop over everyone with for

Here is the main event. Write for person in people: and each time round, one dictionary arrives in person.

What you get is a dictionary, so inside you write person["name"]. However many people there are, the code is the same. Two or a thousand, it is these three lines.

people = [{"name": "Yui", "score": 80}, {"name": "Sou", "score": 55}]for person in people:    print(person["name"], person["score"])

Result

Yui 80
Sou 55

05 / 07

Branch inside the loop

You can use if inside the loop too. Look at one record, decide, produce a result — what you did in chapter 1 now applies to everybody.

This is what it means for a program to become useful. Instead of a person going through a hundred records by eye, the same decision is handed to the machine.

people = [{"name": "Yui", "score": 80}, {"name": "Sou", "score": 55}]for person in people:    if person["score"] >= 60:        print(person["name"], "pass")    else:        print(person["name"], "fail")

Result

Yui pass
Sou fail

06 / 07

Pull the whole lot together

A total or an average comes out the same way. Set up the container first and add as you go round, and that is it.

Do not forget to put the print outside the loop. Inside, and every running figure comes out as well.

people = [{"name": "Yui", "score": 80}, {"name": "Sou", "score": 55}, {"name": "Haru", "score": 90}]total = 0for person in people:    total += person["score"]print(total)print(total / 3)

Result

225
75.0

07 / 07

Add one later on

A register is a list, so the list tools work as they always did. append adds one record. What you hand it is a dictionary.

In other words this shape is what you know about lists laid straight over what you know about dictionaries. There is almost nothing new to learn.

Next comes the last of this group: the tuple, a group you cannot change afterwards.

people = [{"name": "Yui", "score": 80}]people.append({"name": "Sou", "score": 55})for person in people:    print(person["name"])

Result

Yui
Sou