Learning

Zip & Enumerate Utilities

Zip & Enumerate Utilities

zip() Function

python
names = ['Alice', 'Bob', 'Charlie'] ages = [25, 30, 35] paired = list(zip(names, ages)) # [('Alice', 25), ('Bob', 30), ('Charlie', 35)] # With three iterables scores = [95, 87, 92] combined = list(zip(names, ages, scores)) # [('Alice', 25, 95), ('Bob', 30, 87), ('Charlie', 35, 92)]

zip() with Unequal Lengths

python
a = [1, 2, 3, 4, 5] b = ['a', 'b', 'c'] print(list(zip(a, b))) # [(1,'a'), (2,'b'), (3,'c')] — truncated! # zip_longest fills with None (or custom fillvalue) from itertools import zip_longest print(list(zip_longest(a, b))) # [(1,'a'), (2,'b'), (3,'c'), (4,None), (5,None)] print(list(zip_longest(a, b, fillvalue='-'))) # [(1,'a'), (2,'b'), (3,'c'), (4,'-'), (5,'-')]

Unzipping

python
pairs = [('a', 1), ('b', 2), ('c', 3)] letters, numbers = zip(*pairs) print(letters) # ('a', 'b', 'c') print(numbers) # (1, 2, 3) # Transpose a matrix matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] transposed = list(zip(*matrix)) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

enumerate() Function

python
fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits): print(f'{index}: {fruit}') # 0: apple # 1: banana # 2: cherry # Custom start index for i, fruit in enumerate(fruits, start=1): print(f'{i}. {fruit}') # 1. apple # 2. banana # 3. cherry

Practical Patterns

python
# Build dict from two lists keys = ['name', 'age', 'city'] values = ['Alice', 25, 'NYC'] person = dict(zip(keys, values)) # {'name': 'Alice', 'age': 25, 'city': 'NYC'} # Parallel iteration with index names = ['a', 'b', 'c'] scores = [10, 20, 30] for i, (name, score) in enumerate(zip(names, scores)): print(f'#{i+1} {name}: {score}') # Create indexed dict indexed = {i: val for i, val in enumerate(fruits)} # {0: 'apple', 1: 'banana', 2: 'cherry'}
Key Rules
  • •zip() stops at the SHORTEST iterable — silent data loss if lengths differ!
  • •Use itertools.zip_longest() when you need ALL elements from the longest iterable
  • •Unzip with zip(*pairs) — the * unpacks the list of tuples into separate arguments
  • •enumerate() returns (index, value) pairs — much cleaner than manual counter variables
  • •enumerate(iterable, start=N) begins counting from N instead of 0
  • •zip() returns an iterator in Python 3 — wrap in list() to materialize
Your Task

Given two lists `students = ['Alice', 'Bob', 'Charlie', 'Diana']` and `grades = [95, 87, 92]`, do the following: 1) Use zip to pair them (show truncation). 2) Use zip_longest with fillvalue='N/A' to pair all. 3) Build a dict using dict(zip()). 4) Unzip a list of pairs back into two tuples. 5) Use enumerate with start=1 to number the students. 6) Transpose a 3x4 matrix using zip(*matrix).

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should use zip() with students and grades
Should import and use zip_longest
Should use dict(zip()) to build dictionary
Should unzip using zip(*pairs)
Should use enumerate with start=1
Should transpose matrix with zip(*matrix)