Looping on more than one list
Niels Diepeveen
niels at endea.demon.nl
Tue Jul 25 08:19:42 EDT 2000
David Goodger schreef:
>
> on 2000-07-22 12:08, ahmed Bouferguene (aboufer at atlsci.com) wrote:
> > Do no know much of python, but I am wondering whether it is possible
> > to loop
> > on more than one list
>
> A more elegant solution is currently in the works, but for now, you can use:
>
> >>> map(None,"abcd",[1,2,3])
> [('a', 1), ('b', 2), ('c', 3), ('d', None)]
>
> "abcd" is a sequence of 4 items, of course. The above doesn't stop on
> exhaustion of the shortest list though. If it must, you can use:
>
...
> >>> c=[1,2,3,4]
> >>> for (x,y) in map(None,b,c):
> ... if None in (x,y): break # required for uneven sequences,
> # NOT containing None!
Or, more reliably:
for x,y in map(None, b[:len(c)], c[:len(b)]):
--
Niels Diepeveen
Endea automatisering
More information about the Python-list
mailing list