[Tutor] iterating over less than a full list

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Sep 4 19:40:39 CEST 2010


On 04/09/2010 18:29, Sander Sweers wrote:
> On 4 September 2010 19:25, Sander Sweers<sander.sweers at gmail.com>  wrote:
>> for x in some_stuff:
>>     if x<= 10:
>>         print x
>>     else:
>>         break
>
> Oops, corrected version...
>
> count = 0
> for x in some_stuff:
>      if count<  10:
>          print x
>          count +=1
>      else:
>          break
>
> Greets
> Sander
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

I prefer

for i, x in enumerate(some_stuff):
     if i < 10:
         do_it(x)
     else:
         break


Cheers.

Mark Lawrence.



More information about the Tutor mailing list