🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🐍 Python

enumerate and zip

📚 What are enumerate and zip? enumerate() and zip() are built-in functions that make loops more powerful. enumerate() adds a counter to any iterable, giving you both the index and the value. zip() pairs up items from two or more lists so you can process them together in one loop.

5 min 10 XP Lesson 17 of 21
enumerate and zip
🌐

Appy Says…

Tired of writing i = 0 before every loop? Or trying to loop two lists at the same time with clunky index tricks? enumerate and zip are Python's elegant solutions — and once you know them, you'll never go back.

📖

What are enumerate and zip?

enumerate(iterable) wraps an iterable and yields (index, value) pairs — so you get the counter for free. zip(a, b) merges two iterables into pairs — so you can loop both at once.

  • enumerate(['a','b','c'])(0,'a'), (1,'b'), (2,'c')
  • Use: for i, name in enumerate(names):
  • Start at 1: enumerate(names, start=1)
  • zip(['Alice','Bob'], [90, 85])('Alice',90), ('Bob',85)
  • Use: for name, score in zip(names, scores):
  • zip stops at the shortest list — use itertools.zip_longest for unequal lengths
🎮

Think of it like a Roblox scoreboard

Imagine you have a list of players and a separate list of scores. zip pairs them like a scoreboard row: Alice — 450 points. enumerate adds the rank automatically: 1st: Alice — 450 points. No manual counting needed.

⚙️

How It Works

  • 1. enumerate(items) is a lazy iterator — it generates pairs one at a time
  • 2. for i, item in enumerate(items): — Python unpacks each tuple into i and item
  • 3. zip(list1, list2) is also lazy — combines lists element-by-element
  • 4. for a, b in zip(list1, list2): gives you matching pairs on every iteration
  • 5. Both work with any iterable: lists, tuples, strings, generators
  • 6. Convert to a list to see all pairs: list(zip(a, b))
🌍

Real-World Examples

  • Number a leaderboard: for rank, player in enumerate(top_players, start=1): print(f'{rank}. {player}')
  • Match usernames to scores: for user, score in zip(users, scores):
  • Build a dictionary from two lists: dict(zip(keys, values))
  • Display quiz answers: for i, (q, a) in enumerate(zip(questions, answers)):
  • Render UI rows with alternating colours by checking if i % 2 == 0
💡

Key Facts

  • Both enumerate and zip return lazy iterators — wrap in list() to materialise them
  • dict(zip(keys, values)) is the fastest way to build a dictionary from two lists
  • zip(*matrix) transposes a 2D list (swaps rows and columns)
  • Python 3's zip is lazy; Python 2 had izip for the same behaviour
⚠️

Watch Out!

zip silently stops at the shortest list. If your lists have different lengths, you'll lose data from the longer one without any error. Use itertools.zip_longest(a, b, fillvalue=None) when lengths might differ.

📌

Remember

Use enumerate whenever you need both an index and a value. Use zip whenever you need to pair two sequences. Both are cleaner than manual index tracking.

What You Learned

  • enumerate(items) gives (index, value) pairs; zip(a, b) pairs two iterables
  • Both are lazy iterators — wrap in list() to see all pairs at once
  • Unlocks: clean loop counters, building dicts from lists, processing parallel data streams

Key Facts

  • Both enumerate and zip return lazy iterators — wrap in list() to materialise them
  • dict(zip(keys, values)) is the fastest way to build a dictionary from two lists
  • zip(*matrix) transposes a 2D list (swaps rows and columns)
  • Python 3's zip is lazy; Python 2 had izip for the same behaviour

Real-World Examples

• Number a leaderboard: <code>for rank, player in enumerate(top_players, start=1): print(f'{rank}. {player}')</code> • Match usernames to scores: <code>for user, score in zip(users, scores):</code> • Build a dictionary from two lists: <code>dict(zip(keys, values))</code> • Display quiz answers: <code>for i, (q, a) in enumerate(zip(questions, answers)):</code> • Render UI rows with alternating colours by checking if <code>i % 2 == 0</code>

Remember

Use enumerate whenever you need both an index and a value. Use zip whenever you need to pair two sequences. Both are cleaner than manual index tracking.

Quick Quiz

1 / 2

enumerate gives?