[Tutor] iterating over a sequence question..
Iyer
maseriyer at yahoo.com
Sun Jun 17 08:59:47 CEST 2007
say, if I have a list l = [1,2,3,5]
and another tuple t = ('r', 'g', 'b')
Suppose I iterate over list l, and t at the same time, if I use the zip function as in zip(l,t) , I will not be able to cover elements 3 and 5 in list l
>>> l = [1,2,3,5]
>>> t = ('r', 'g', 'b')
>>> for i in zip(l,t):
... print i
...
(1, 'r')
(2, 'g')
(3, 'b')
is there an elegant way in python to print all the elements in l, while looping over list t, if len(t) != len(l) as to get the output:
(1, 'r')
(2, 'g')
(3, 'b')
(5, 'r')
I could iterate over l only and reset the index to point to the first element of t, in case the elements in t are "exhausted" . Any pythonic way to iterate over a sequence, while iterating over another shorter sequence continously (granted that the lengths of the two lists are different)?
-iyer
---------------------------------
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070616/c0f1c047/attachment.html
More information about the Tutor
mailing list