Python enumerate(): Return index and value — Syntax, start offset, iterables, unpacking, zip comparison, performance tips, pitfalls, and practical examples > dev

Skip to content

Entire search within the site

👈Go back dev

Python Python enumerate(): Return index and value — Syntax, start offset, ite…

page info

name Goposu datetime⏰ 25-10-11 17:38 hit👁️ 9 comment💬 0

text

Python enumerate(): Return index and value — Syntax, start offset, iterables, unpacking, zip comparison, performance tips, pitfalls, and practical examples

Python enumerate(): Pair each item with its index

The built-in enumerate() function iterates over an iterable and yields pairs of (index, value). It is the idiomatic way to track positions while looping, replacing manual counters and reducing off-by-one mistakes. This guide covers syntax, start offsets, common patterns, comparisons with zip(), pitfalls, and performance tips.

Syntax

enumerate(iterable, start=0)
  • iterable: Any object you can iterate (list, tuple, string, generator, file, etc.).
  • start: The first index value (default 0); useful for 1-based indexing or offset windows.

Quick examples

names = ["Alice", "Bob", "Cleo"]
Default: index starts at 0
for i, name in enumerate(names): print(i, name)        # 0 Alice, 1 Bob, 2 Cleo
Custom start index
for i, name in enumerate(names, start=1): print(i, name)        # 1 Alice, 2 Bob, 3 Cleo

Core patterns

  • Find with index: Stop when a condition is met.
  • Track positions: Log row numbers or error offsets during processing.
  • Unpack pairs: Keep code clean by unpacking (i, x) directly in the loop.
# First match indexnums = [4, 9, 12, 7, 15]for i, x in enumerate(nums):if x % 3 == 0:print("First multiple of 3 at index:", i)break
Logging with positions
for i, line in enumerate(open("data.txt"), start=1): if not line.strip(): print(f"Empty line at {i}")

Comparison with zip()

enumerate() pairs positions with values automatically. zip() pairs two or more iterables together. Combine them to track index while zipping multiple sequences.

a = ["x", "y", "z"]b = [10, 20, 30]for i, (x, y) in enumerate(zip(a, b), start=1):print(i, x, y)        # 1 x 10, 2 y 20, 3 z 30

Advanced usage

  • Slice and enumerate: Apply to subranges using slicing.
  • Enumerate generators: Works lazily; indexes arrive on demand.
  • Preserve index across chunks: Offset start when processing in windows.
data = list(range(100))
Slice with local index
for j, x in enumerate(data[20:30], start=20): print(j, x)
Enumerate over a generator
def gen(): for k in range(3): yield k*k for i, v in enumerate(gen(), start=100): print(i, v)           # 100 0, 101 1, 102 4

Pitfalls and how to avoid them

  • Mutating the iterable: Changing a list while iterating can desync indices; avoid in-place additions/removals during the loop.
  • Multiple sources of truth: Don’t manually increment a counter alongside enumerate(); rely on i only.
  • Index needs: If you only need values, skip enumerate() for clarity; if you need positions, prefer enumerate() over manual counters.
# Bad: manual index + enumeratei = 0for i, x in enumerate([10, 20, 30]):i += 1    # confusing; don't do this
Good: use i from enumerate only
for i, x in enumerate([10, 20, 30], start=1): pass

Performance notes

  • Zero-copy indexing: enumerate() is a lightweight iterator; it avoids building temporary index lists.
  • Generators: Works efficiently with large streams; index increments are constant-time.
  • Vectorized data: For NumPy arrays, prefer array operations; use enumerate() when pure Python iteration is required.

Practical examples

# 1) Tag items with positionsitems = ["taskA", "taskB", "taskC"]tagged = [f"{i}:{x}" for i, x in enumerate(items, start=1)]print(tagged)  # ['1:taskA', '2:taskB', '3:taskC']
2) Stable renumbering after filter
values = [3, 8, 13, 2, 21] odd = [x for x in values if x % 2] for rank, val in enumerate(odd, start=1): print(rank, val)
3) Group with index metadata
records = ["ok", "warn", "ok", "fail"] errors = [(i, r) for i, r in enumerate(records) if r == "fail"] print(errors)  # [(3, 'fail')]

FAQ

Can I start indexing at 1?
Yes. Pass start=1 to enumerate().
Does enumerate work on files and generators?
Yes. It yields index/value pairs lazily, ideal for large inputs.
How do I keep indices aligned after slicing?
Use a matching start offset (e.g., slice start) to reflect original positions.

Related keywords

Python enumerate, index and value, start offset, zip vs enumerate, lazy iteration, generator indexing, list processing, slicing

👍good0 👎nogood 0

comment list 0

There are no registered comments.

이미지 목록

전체 19건 1 페이지
게시물 검색
Copyright © https://goposu.com All rights reserved.
View PC version