Enumerate & Zip
Pair indices and parallel sequences elegantly. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Indexed ListingStudy Material
Read the full lesson
Enumerate for index and value
Use enumerate() when you need a counter and the item itself.
pythonnames = ["Ali", "Mona", "Sara"] for i, name in enumerate(names): print(i, name)
Starting from 1
You can start counting at any number.
pythonfor i, name in enumerate(names, start=1): print(i, name)
Zip for parallel lists
zip() pairs items from multiple lists.
pythonletters = ["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.
pythonpairs = [("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.
pythonkeys = ["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.
Indexed Listing
Use the enumerate structure to label sequence instances.
Pairing Coordinates
Parallel process arrays seamlessly executing zip features.
Dictionary Building via Zip
Instantiate dicts combining external label and data sequences.
Filtering over Zip Space
Enforce selective pairing behaviors bridging two datasets.
Enumerating Dict Iteration
Combine enumerate mapping atop dictionary structures.