Stop 03 · Control FlowTopics · 8/8

enumerate() & zip()

Part of the Python Roadmap

In short

Need the position and the item? Need to walk through two lists together? Beginners reach for range(len(...)) and index juggling. enumerate() and zip() do the same job in cleaner, safer code.

enumerate() gives you the position and the item together while looping, and zip() walks through two or more sequences side by side. Both replace error-prone range(len(...)) loops.

The jaadu leads two rows of children marching in pairs across a school ground, holding up fingers to count each pair as they pass.

Think of it like this

Partners in a school march

On sports day, students march in pairs: one from the left line, one from the right, side by side. That's zip(). The teacher at the front calls out "Pair 1, pair 2, pair 3" as they pass. That's enumerate(). When one line runs out of students, the marching stops.

Watch it happen

Index juggling vs enumerate and zip

range(len(...))

  • for i in range(len(names)):
  • print(i + 1, names[i], marks[i])
  • Easy to mix up indexes or go out of range
  • Harder to read at a glance

enumerate + zip

  • for rank, (name, mark) in
  • enumerate(zip(names, marks), start=1):
  • Each value has a clear name
  • No index arithmetic to get wrong

Prefer enumerate when you need a counter and zip when you walk lists side by side.

The big ideas

What you need to know

enumerate gives (index, item)

for i, item in enumerate(items) hands you both. Add start=1 for human-friendly numbering.

zip pairs items up

zip(names, marks) gives ("Kavya", 96), ("Arjun", 91) and so on. It works with three or more lists too.

zip stops at the shortest

Extra items in a longer list are silently dropped. Use zip(a, b, strict=True) (Python 3.10+) to raise an error instead.

dict(zip(keys, values))

Two parallel lists become a dictionary in one line, a very common trick when reading data.

See it in code

Walk through the code

python
1toppers = ["Kavya", "Arjun", "Sneha"]2for rank, name in enumerate(toppers, start=1):3    print(f"{rank}. {name}")4 5students = ["Kavya", "Arjun", "Sneha"]6marks = [96, 91, 89]7for name, score in zip(students, marks):8    print(f"{name}: {score}")9 10report = dict(zip(students, marks))11print(report)   # {'Kavya': 96, 'Arjun': 91, 'Sneha': 89}12 13for i, (name, score) in enumerate(zip(students, marks), start=1):14    print(i, name, score)

1/4enumerate with start=1 prints 1. Kavya, 2. Arjun, 3. Sneha.

Numbered lists, side-by-side loops and a quick dictionary, with no index arithmetic.

Save yourself the pain

Common mistakes

Oops

Writing for i in range(len(names)): print(names[i]).

Do this instead

Use for name in names, or for i, name in enumerate(names) if you need the position.

Oops

Zipping lists of different lengths and losing data without noticing.

Do this instead

Check lengths first, or pass strict=True so Python raises an error on a mismatch.

Oops

Writing for i, name, score in enumerate(zip(...)) and getting a ValueError.

Do this instead

enumerate gives (index, pair), so unpack the pair in brackets: for i, (name, score) in ...

Your mission

Class report

~20 min

Quick check

What does list(zip([1, 2, 3], ["a", "b"])) return?

Go deeper

  1. 1Print a numbered list of students with enumerate(names, start=1).
  2. 2Pair names with marks using zip() and print each pair.
  3. 3Build a dict from two lists with dict(zip(keys, values)).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge