3 ms·
Check out more-itertools which has things like take, tail and a bunch of other nice functions for working with iterators if you haven’t. Would allow you to do
by memco 5y ago
Check out more-itertools which has things like take, tail and a bunch of other nice functions for working with iterators if you haven’t. Would allow you to do something like
for el in take(100, iter):
print(el)
Plus there’s a bunch of other things in there for windowing, finding out whether or where empty stuff in the generator are.
https://more-itertools.readthedocs.io/ https://more-itertools.readthedocs.io/
- whalesalad 5y agoI use this lib for this exact reason! Great suggestion. I wish the standard library had more functional functions like this. head/tail/take etc
- masklinn 5y agoIt… does? They’re called next, islice, and islice. more_itertools is nice but none of those 3 is a compelling argument for it.
- lou1306 5y agoBTW you can get head and tail with plain old unpacking :) >>> head, *tail = range(3) >>> head 0 >>> tail [1, 2] Granted, unpacking also coalesces lazy iterables (like range in this case) into lists, but if you aren't working with large/infinite iterables you should be alright.