🚀 Alguni Start learning

Unit 6: Lists & Dictionaries

Collections that grow.

Unit 6 of 31 in Python for kids. Its 5 lessons are Growing Lists, Asking Lists Questions, Dictionaries, Looping Over Dictionaries and Pet Shop — below is everything each one explains, and a question or two from it to try.

Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

📈 Growing Lists

Lists can change

Unlike a word, a list can grow after you make it. append sticks something on the end.

Python

pets = ["cat"]
pets.append("dog")
print(pets)

It prints

['cat', 'dog']

Taking things out

remove takes out the first matching item. pop takes one out by position and hands it back to you.

Python

pets = ["cat", "dog", "fish"]
pets.remove("dog")
print(pets)
last = pets.pop()
print(last)
print(pets)

It prints

['cat', 'fish']
fish
['cat']

Try it yourself

What does this print?

Python

nums = [1, 2]
nums.append(3)
print(len(nums))

What does pets.pop() do with no number in the brackets?

  • Empties the whole list
  • Removes and hands back the LAST item
  • Removes the first item
  • Nothing

Answer them in the app

🔍 Asking Lists Questions

Is it in the list?

The same in that searched inside a word also searches a list. And .index() tells you *where* something is.

Python

pets = ["cat", "dog", "fish"]
print("dog" in pets)
print(pets.index("fish"))

It prints

True
2

Putting things in order

sorted() hands back a new sorted list and leaves the original alone. .sort() rearranges the list itself and hands back nothing.

Python

nums = [3, 1, 2]
print(sorted(nums))
print(nums)
nums.sort()
print(nums)

It prints

[1, 2, 3]
[3, 1, 2]
[1, 2, 3]

Try it yourself

What does this print?

Python

nums = [4, 8, 15]
print(16 in nums)

Why does nums = nums.sort() lose your list?

  • sort() is broken
  • sort() rearranges in place and hands back None, so you overwrite the list with None
  • You need sorted() twice
  • Lists cannot be sorted

Answer them in the app

📖 Dictionaries

Look it up by name

In a real dictionary you look up a *word* and get its *meaning*. A Python dictionary works the same way: look something up by its name — the key — and get its value. No counting positions.

Python

sounds = {"cat": "meow", "dog": "woof"}
print(sounds["cat"])

It prints

meow

Adding and changing

Assign to a key that does not exist yet and it gets added. Assign to one that does and it gets replaced.

Python

scores = {"Sam": 5}
scores["Kim"] = 8
scores["Sam"] = 10
print(scores)

It prints

{'Sam': 10, 'Kim': 8}

Asking for something missing

Ask for a key that is not there and Python stops with an error. .get() is the polite version — it hands back nothing, or a fallback you choose.

Python

ages = {"Sam": 9}
print(ages.get("Kim"))
print(ages.get("Kim", 0))

It prints

None
0

Try it yourself

What is the main difference from a list?

  • Dictionaries can only hold numbers
  • You look things up by a name you chose, not by position
  • Dictionaries cannot change
  • There is no difference

What does this print?

Python

ages = {"Sam": 9, "Kim": 11}
print(ages["Kim"])

Answer them in the app

🔁 Looping Over Dictionaries

Both halves at once

.items() hands you the key and the value on every turn of the loop, so you can name them both.

Python

sounds = {"cat": "meow", "dog": "woof"}
for animal, noise in sounds.items():
    print(animal + " says " + noise)

It prints

cat says meow
dog says woof

Just the keys, or just the values

Loop over the dictionary itself and you get the keys. .values() gives just the values.

Python

ages = {"Sam": 9, "Kim": 11}
for name in ages:
    print(name)
print(sum(ages.values()))

It prints

Sam
Kim
20

Try it yourself

What does this print?

Python

scores = {"Sam": 3, "Kim": 5}
total = 0
for name, score in scores.items():
    total = total + score
print(total)

Answer it in the app

🏆 Pet Shop

Try it yourself

What does this print?

Python

shop = {"cat": 2, "dog": 0}
print(shop.get("fish", 0))

What does this print?

Python

stock = {"cat": 2, "dog": 0, "fish": 5}
for animal, count in stock.items():
    if count > 0:
        print(animal)

Answer them in the app