Fill in

Recent Sightings

Two more treasures from collections:

namedtuple: a tuple with field names

from collections import namedtuple

Island = namedtuple("Island", ["name", "x", "y"])
home = Island("Amber Isle", 3, 4)
print(home.name, home.x)     # Amber Isle 3
name, x, y = home            # still a tuple: unpack it

home.name is far easier to read than home[0]. Like any tuple, it can't be changed after creation.

deque: a double-ended queue

A deque ("deck") adds and removes from both ends quickly: append, appendleft, pop, popleft. Give it a maxlen and it keeps only the newest items, dropping old ones automatically:

from collections import deque

last3 = deque(maxlen=3)
for n in [1, 2, 3, 4, 5]:
    last3.append(n)
print(list(last3))           # [3, 4, 5]

Assignment

Fill in the four ___ blanks so the program keeps only the 3 most recent sightings and prints their creatures: ['gull', 'kraken', 'otter'].