Lesson 6 of 10
Parallel lists
- 10 min
- 5 checks
- 3 hints
- Go to task
Goal
zip and enumerate instead of index numbers
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.
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- grades maps each name to its score
- lines is numbered in the required format
- Numbering starts at 1, not 0
- grades is built with dict(zip(...))
- Numbered with enumerate, not range(len(...))