For Loop in List

Tim Chase python.list at tim.thechases.com
Sun Jan 13 09:07:10 EST 2013


On 01/13/13 07:48, Boris FELD wrote:
> 2013/1/13 Tim Chase <python.list at tim.thechases.com>:
>>>>> SIZE = 3
>>>>> for i in range(len(list1)//SICE):
>> ...     print list1[i*SIZE:i*SIZE+SIZE]
>
> A little shorter and simpler version:
>>>> x = x[1:]
>>>> for i in range(0,len(x),SIZE):
> ...      print x[i: i+SIZE]

Doh, I always forget that range() takes an optional stride.  Or 
rather, I use it so infrequently that I reach for other alternatives 
before it occurs to me :)

-tkc






More information about the Python-list mailing list