I’m looking for a pythonic way of iterating over first n items of an iterable (upd: not a list in a common case, as for lists things are trivial), and it’s quite important to do this as fast as possible. This is how I do it now:
count = 0
for item in iterable:
do_something(item)
count += 1
if count >= n: break
Doesn’t seem neat to me. Another way of doing this is:
for item in itertools.islice(iterable, n):
do_something(item)
This looks good, the question is it fast enough to use with some generator(s)? For example:
pair_generator = lambda iterable: itertools.izip(*[iter(iterable)]*2)
for item in itertools.islice(pair_generator(iterable), n):
so_something(item)
Will it run fast enough as compared to the first method? Is there some easier way to do it?
for item in itertools.islice(iterable, n):is the most obvious, easy way to do it. It works for arbitrary iterables and is O(n), like would be any sane solution.It’s conceivable that another solution could have better performance; we wouldn’t know without timing. I wouldn’t recommend bothering with timing unless you profile your code and find this call to be a hotspot. Unless it’s buries within an inner loop, it is highly doubtful that it will be. Premature optimization is the root of all evil.
If I was going to look for alternate solutions, I would look at ones like
for count, item in enumerate(iterable): if count > n: break ...andfor i in xrange(n): item = next(iterator) .... I wouldn’t guess these would help, but they seem to be worth trying if we really want to compare things. If I was stuck in a situation where I profiled and found this was a hotspot in an inner loop (is this really your situation?), I would also try to ease the name lookup from getting theisliceattribute of the globaliteroolsto binding the function to a local name already.These are things you only do after you’ve proven they’ll help. People try doing them other times a lot. It doens’t help make their programs appreciably faster; it just makes their programs worse.