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

Andre Müller gbs.deadeye at gmail.com
Wed Jun 14 15:28:50 EDT 2017


I'm a fan of infinite sequences. Try out itertools.islice.
You should not underestimate this very important module.

Please read also the documentation:
https://docs.python.org/3.6/library/itertools.html

from itertools import islice

iterable = range(10000000000)
# since Python 3 range is a lazy evaluated object
# using this just as a dummy
# if you're using legacy Python (2.x), then use the xrange function for it
# or you'll get a memory error

max_count = 10
step = 1

for i, element in enumerate(islice(iterable, 0, max_count, step), start=1):
    print(i, element)


Greetings
Andre



More information about the Python-list mailing list