I propose adding an optional limit parameter to the built-in enumerate() function to restrict the number of iterations.
Limiting iteration is a common task, but current solutions are verbose:
# Current approach: requires import and nesting
from itertools import islice
for i, item in islice(enumerate(data), 5):
...
Alternatives have downsides: slicing creates copies, and zip(range(n), data) ignores the start parameter.
Update the signature to: enumerate(iterable, start=0, limit=None)
Examples:
# Stop after 10 items
for i, line in enumerate(file, limit=10):
print(i, line)
# Works with start
for i, item in enumerate(data, start=1, limit=3):
print(i, item)
It makes a common pattern more readable and removes the need for itertools.islice in simple loops.
Thoughts?