Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. range() / enumerate() / zip()

range() / enumerate() / zip()

Built-in functions for loop operations: generating sequences, adding indexes, and processing multiple lists in parallel.

Syntax

# Generates a sequence of consecutive integers.
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

FunctionDescription
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

# Use range() to repeat a for loop.
for i in range(5):
    print(i, end=" ")  # 0 1 2 3 4

# Specify a start, stop, and step.
for i in range(2, 10, 2):
    print(i, end=" ")  # 2 4 6 8

# Iterate in reverse using range().
for i in range(5, 0, -1):
    print(i, end=" ")  # 5 4 3 2 1

# Convert range() to a list.
nums = list(range(1, 6))
print(nums)  # [1, 2, 3, 4, 5]

# Use enumerate() to get both the index and value at once.
fruits = ["apple", "orange", "grape"]
for i, fruit in enumerate(fruits):
    print(f"{i}: {fruit}")
# 0: apple
# 1: orange
# 2: grape

# Use start=1 to begin the index at 1.
for i, fruit in enumerate(fruits, start=1):
    print(f"#{i}: {fruit}")
# #1: apple

# Use zip() to process multiple lists in parallel.
names = ["Alice", "Bob", "Carol"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
    print(f"{name}: {score} points")
# Alice: 85 points

# Use zip() to create a dictionary.
person = dict(zip(["name", "age", "city"], ["Alice", 20, "Tokyo"]))
print(person)  # {'name': 'Alice', 'age': 20, 'city': 'Tokyo'}

# Use reversed() to iterate over a list in reverse.
for item in reversed(fruits):
    print(item, end=" ")  # grape orange apple

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, use itertools.zip_longest() instead. 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 .