Dirk Dirk - 1 year ago 71
Python Question

Most Pythonic for / enumerate loop?

I have a loop condition:

for count, item in enumerate(contents):

but only want to use the first 10 elements of contents. What is the most pythonic way to do so?

Doing a:

if count == 10: break

seems somewhat un-Pythonic. Is there a better way?

Answer Source

Use a slice (slice notation)

for count, item in enumerate(contents[:10]):

If you are iterating over a generator, or the items in your list are large and you don't want the overhead of creating a new list (as slicing does) you can use islice from the itertools module:

for count, item in enumerate(itertools.islice(contents, 10)):

Either way, I suggest you do this in a robust manner, which means wrapping the functionality inside a function (as one is want to do with such functionality - indeed, it is the reason for the name function)

import itertools

def enum_iter_slice(collection, slice):
    return enumerate(itertools.islice(collection, slice))


>>> enum_iter_slice(xrange(100), 10)
<enumerate object at 0x00000000025595A0>
>>> list(enum_iter_slice(xrange(100), 10))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
>>> for idx, item in enum_iter_slice(xrange(100), 10):
    print idx, item

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

If you were using enumerate and your count variable just to check the items index (so that using your method you could exit/break the loop on the 10th item.) You don't need enumerate, and just use the itertools.islice() all on its own as your function.

for item in itertools.islice(contents, 10):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download