[Tutor] iterating over a sequence question..

Alan Gauld alan.gauld at btinternet.com
Sun Jun 17 16:38:59 CEST 2007


"Luke Paireepinart" <rabidpoobear at gmail.com> wrote

> The first thing that occurred to me was just to use a modulus to 
> index
> into the second, shorter list.

That was the first thing that occured to me too but when
I tried it I couldn't get it to work...

> >>> l = [1,2,3,4,5]
> >>> t = ('r','g','b')
> >>> for i in range(len(l)):
>    print (l[i], t[i%len(t)])

because I was trying t[len(t) % i]
and various  variations, I never thought of reversing the arguments!"
I knew a modulus trick existed but couldn't figure it out this 
morning.

I hadn't had coffee yet, at least that's my excuse! :-)

Alan G.




More information about the Tutor mailing list