Legends ofPythos
Claim your name
Pythonic Python

enumerate and zip

Lesson 1 of 12

Watch the lesson0:58 · with Sigrid
Think of a list as a conveyor belt carrying items to your workstation. Sometimes, you need both the item and its position on that belt—like printing line numbers in a report or matching names with scores. The old habit is reaching for range(len(my_list)) to count manually. That works, but it makes your code noisier and harder to read.

enumerate()

tasks = ['write', 'test', 'deploy']
for i, t in enumerate(tasks):
    print(f'{i}: {t}')
Prints each task with its index
enumerate wraps your list into pairs of (index, value). You unpack them directly in the loop header. This is cleaner than manual counting because Python handles the indexing for you.
tasks = ['write', 'test', 'deploy']
for i, t in enumerate(tasks, start=1):
    print(f'{i}. {t}')
Starts numbering from 1
The start argument lets you change where counting begins. This is useful for human-facing output, like numbered lists in a UI or report.

zip()

names = ['Ada', 'Grace']
scores = [92, 85]
for n, s in zip(names, scores):
    print(f'{n} scored {s}')
Pairs names with their scores
zip glues two lists together element by element. It stops when the shorter list runs out, so you never hit an IndexError. This makes parallel iteration safer and cleaner than manual indexing.
You now have the tools to write functions like numbered(items) using enumerate, or pair(names, scores) using zip. These patterns show up constantly: numbered receipts, paired sensor readings, ranked leaderboards. Your code will be shorter and less error-prone.

Your turn

0 of 3 solved

Exercise 1

+30 XP
Write a function numbered(items) that returns a list of strings where each item is prefixed with its index, starting from 1. Use enumerate with the start argument. For example: ['a', 'b'] becomes ['1: a', '2: b'].
def numbered(items):
    pass

Run your code to check it against the tests.

Exercise 2

+30 XP
Write a function pair(names, scores) that zips two lists together and returns a list of tuples. For example: (['A', 'B'], [90, 85]) becomes [('A', 90), ('B', 85)]. If one list is shorter, stop at the shortest.
def pair(names, scores):
    pass

Run your code to check it against the tests.

Exercise 3

+30 XP
Write a function leaderboard(names, scores) that pairs each name with its score using zip, numbers the pairs from 1 using enumerate, and returns a list of strings in the form '1. Ada - 92'. If one list is shorter, stop at the shorter one.

Example: (['Ada', 'Grace'], [92, 85]) returns ['1. Ada - 92', '2. Grace - 85'].
def leaderboard(names, scores):
    pass

Run your code to check it against the tests.