Lesson 6 of 10

Parallel lists

Goal

zip and enumerate instead of index numbers

01

Explanation

When you have two parallel lists, like a list of names and a list of scores, you may be tempted to write for i in range(len(names)) and then names[i] and scores[i]. That works, but it drags in index numbers you don't need, and they're easy to get wrong.

zip(names, scores) walks both lists together and gives you a pair on each pass: for name, score in zip(names, scores). And dict(zip(keys, values)) builds a dict from two lists in one line. If you also need to number the items, enumerate(items, start=1) gives you a number along with each item, counting from whatever number you choose.

Watch out: zip stops at the end of the shorter list without telling you, so an extra item in one of them is lost without any error. zip(a, b, strict=True) raises ValueError instead of staying quiet.

02

Your task

Build the dict grades from names and scores using dict(zip(...)). Then build lines, a list of strings numbered from 1 in the form "1. Layan: 91", using enumerate and zip together.

Checks

5 checks
  1. grades maps each name to its score
  2. lines is numbered in the required format
  3. Numbering starts at 1, not 0
  4. grades is built with dict(zip(...))
  5. Numbered with enumerate, not range(len(...))
main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.