Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 03 / Topic 10 of 12

enumerate() and zip()

  • enumerate(items, start=1) gives you the position and the item together.
  • zip(a, b) walks over two lists side by side, like the two sides of a zip. It stops at the shorter one.

Real-world example: a class rank list and a price list.

toppers = ["Asha", "Ravi", "Meena"]
for rank, name in enumerate(toppers, start=1):
    print(f"{rank}. {name}")
# 1. Asha
# 2. Ravi
# 3. Meena

products = ["Pen", "Notebook", "Bag"]
prices = [10, 45, 799]
for product, price in zip(products, prices):
    print(f"{product}: ₹{price}")
# Pen: ₹10
# Notebook: ₹45
# Bag: ₹799

Avoid for i in range(len(items)): when you only need the items. Use for item in items or enumerate.

🔍 Going deeper: zip() can take three or more lists. Since Python 3.10, zip(a, b, strict=True) raises an error if the lengths differ, which catches bugs where one list is missing an item.

Done here? The practice set for Part 3 — 7 exercises with solutions — is on the part page.

Search the guide