Standard lib version of something like enumerate() that takes a max count iteration parameter?

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Wed Jun 14 14:38:19 EDT 2017


Malcolm Greene writes:

> Wondering if there's a standard lib version of something like
> enumerate() that takes a max count value?
> Use case: When you want to enumerate through an iterable, but want to
> limit the number of iterations without introducing if-condition-break
> blocks in code.
> Something like:
>
> for counter, key in enumerate( some_iterable, max_count=10 ):
>     <loop logic here>
>
> Thank you,
> Malcolm

for counter, key in zip(range(10), some_iterable):
    <loop logic here>



More information about the Python-list mailing list