range() / enumerate() / zip()
| Since: | Python 2(2000) |
|---|
Built-in functions for loop operations: generating sequences, adding indexes, and processing multiple lists in parallel.
Syntax
range(stop) range(start, stop) range(start, stop, step) # Returns each element of an iterable paired with its index. enumerate(iterable, start=0) # Combines multiple iterables element by element. zip(iterable1, iterable2, ...) # Returns an iterable in reverse order. reversed(sequence)
Function List
| Function | Description |
|---|---|
| range(stop) / range(start, stop, step) | Returns an object that generates integers over the specified range. Commonly used with for loops or list(). |
| enumerate(iterable, start=0) | Returns each element of an iterable as an (index, value) tuple. Use start to set the starting index. |
| zip(*iterables) | Returns tuples pairing elements at the same position from multiple iterables. Stops when the shortest iterable is exhausted. |
| reversed(seq) | Returns an iterator that yields items from a sequence (list, tuple, string, etc.) in reverse order. |
Sample Code
range_basic.py
for i in range(5):
print(i, end=" ")
print()
for i in range(2, 10, 2):
print(i, end=" ")
print()
for i in range(5, 0, -1):
print(i, end=" ")
print()
nums = list(range(1, 6))
print(nums)
Running the code produces the following output:
python3 range_basic.py 0 1 2 3 4 2 4 6 8 5 4 3 2 1 [1, 2, 3, 4, 5]
enumerate_example.py
pilots = ["Ikari Shinji", "Ayanami Rei", "Soryu Asuka"]
for i, pilot in enumerate(pilots):
print(f"{i}: {pilot}")
print()
for i, pilot in enumerate(pilots, start=1):
print(f"#{i}: {pilot}")
Running the code produces the following output:
python3 enumerate_example.py 0: Ikari Shinji 1: Ayanami Rei 2: Soryu Asuka #1: Ikari Shinji #2: Ayanami Rei #3: Soryu Asuka
zip_reversed_example.py
names = ["Ayanami Rei", "Ikari Shinji", "Soryu Asuka"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
print(f"{name}: {score} points")
print()
person = dict(zip(["name", "age", "org"], ["Ayanami Rei", 14, "NERV"]))
print(person)
print()
for item in reversed(names):
print(item, end=" ")
print()
Running the code produces the following output:
python3 zip_reversed_example.py
Ayanami Rei: 85 points
Ikari Shinji: 92 points
Soryu Asuka: 78 points
{'name': 'Ayanami Rei', 'age': 14, 'org': 'NERV'}
Soryu Asuka Ikari Shinji Ayanami Rei
Common Mistakes
Common Mistake 1: Expecting range() to include the stop value
range(5) generates values from 0 to 4. The value 5 is not included. To loop from 1 through 5, write range(1, 6).
for i in range(5):
print(i)
The same logic can also be written as:
for i in range(1, 6):
print(i)
Common Mistake 2: Expecting zip() to process all elements from lists of different lengths
zip() stops when the shortest iterable is exhausted. To process all elements from iterables of different lengths, you can use itertools.zip_longest().
names = ["Ikari Shinji", "Ayanami Rei", "Soryu Asuka"]
scores = [85, 92]
for name, score in zip(names, scores):
print(f"{name}: {score} points")
The same logic can also be written as:
from itertools import zip_longest
names = ["Ikari Shinji", "Ayanami Rei", "Soryu Asuka"]
scores = [85, 92]
for name, score in zip_longest(names, scores, fillvalue=0):
print(f"{name}: {score} points")
Notes
range() returns a lazy-evaluated object that is highly memory-efficient. Even range(1000000) does not load a million integers into memory at once — it generates values on demand. The stop value is not included in the result (e.g., range(5) yields 0 through 4; 5 is not included).
enumerate() eliminates the need to manage indexes manually. Writing for i, fruit in enumerate(fruits): is considered more Pythonic than for i in range(len(fruits)):.
zip() stops when the shortest iterable is exhausted. If you need to process all elements from iterables of different lengths, you can use itertools.zip_longest(). zip() is also commonly used to loop over multiple lists together, and is often paired with sorted() for sorting operations.
If you find any errors or copyright issues, please contact us.