[Tutor] iterating over less than a full list
Dave Angel
davea at ieee.org
Sat Sep 4 21:07:16 CEST 2010
Bill Allen wrote:
> Say I have and iterable called some_stuff which is thousands of items in
> length and I am looping thru it as such:
>
> for x in some_stuff
> etc...
>
> However, what if I want only to iterate through only the first ten items of
> some_stuff, for testing purposes. Is there a concise way of specifying that
> in the for statement line?
>
>
> -Bill
>
>
You could use islice()
import itertools
for x in itertools.islice(some_stuff, 0, 10)
print x
DaveA
More information about the Tutor
mailing list