IntermediateLesson firstCategory 9 of 20

Enumerate & Zip

Pair indices and parallel sequences elegantly. Read the lesson first, then move through the exercises in order.

7 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Indexed Listing

Study Material

Read the full lesson

Enumerate for index and value

Use enumerate() when you need a counter and the item itself.

python
names = ["Ali", "Mona", "Sara"] for i, name in enumerate(names): print(i, name)

Starting from 1

You can start counting at any number.

python
for i, name in enumerate(names, start=1): print(i, name)

Zip for parallel lists

zip() pairs items from multiple lists.

python
letters = ["a", "b", "c"] numbers = [1, 2, 3] for letter, number in zip(letters, numbers): print(letter, number)

zip() stops at the shortest list.

Unzipping with the star operator

You can turn pairs back into two lists.

python
pairs = [("x", 1), ("y", 2)] letters, numbers = zip(*pairs) print(list(letters)) print(list(numbers))

Build a dict with zip

This is a clean way to combine keys and values.

python
keys = ["name", "age"] values = ["Lina", 28] user = dict(zip(keys, values))

Common mistakes to avoid

  • Forgetting that enumerate() starts at 0 by default.
  • Assuming zip() will keep going past the shortest list.
  • Forgetting to convert the result of zip() into a list if you want to print it.

What you should understand after this lesson

  • How to use enumerate() for index + value.
  • How to use zip() to walk lists together.
  • How to build dictionaries from paired data.

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.