[Tutor] iterating over less than a full list
wallenpb at gmail.com
Sun Sep 5 04:00:38 CEST 2010
Thanks to everyone who replied. Some of the methods presented where some I
had thought of, others were new to me. Particularly, I did not realize I
could apply a slice to a list. The for x in some_stuff[:value] form worked
very well for my purposes. I can also see I need to look into the itertools
module and see what goodies are to be found there.
On Sat, Sep 4, 2010 at 2:07 PM, Dave Angel <davea at ieee.org> wrote:
> 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
>> However, what if I want only to iterate through only the first ten items
>> some_stuff, for testing purposes. Is there a concise way of specifying
>> in the for statement line?
> You could use islice()
> import itertools
> for x in itertools.islice(some_stuff, 0, 10)
> print x
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor