Code

enumerate & zip

Code like for i in range(len(items)): works, but Python has nicer tools.

enumerate gives you a counter and the item:

for i, name in enumerate(["Ada", "Bo"], start=1):
    print(i, name)      # 1 Ada / 2 Bo

zip walks several sequences side by side:

islands = ["Moss", "Ash"]
keepers = ["Ada", "Bo"]
for island, keeper in zip(islands, keepers):
    print(f"{keeper} guards {island}")

dict(zip(islands, keepers))   # {'Moss': 'Ada', 'Ash': 'Bo'}

Careful: zip silently stops at the shortest input. If the lengths should match, use zip(a, b, strict=True), which raises ValueError when they don't, so mistakes can't hide.

Assignment

  1. leaderboard(names) returns a list of strings numbered from 1: ["1. Ada", "2. Bo", ...].
  2. pair_up(islands, keepers) returns a dict mapping each island to its keeper. If the lists have different lengths it must raise ValueError.