for loop possible enhancement

Will Ware wware at world.std.com
Fri Sep 22 20:44:01 CEST 2000


bob van der Poel (bvdpoel at uniserve.com) wrote:
> What
> I'm wondering is if it might be possible to add to the for syntax
> something like:
> 	for a in [1,2,3,4], b in [a,b,c,d]:

Try this:
>>> for x, y in map(None, [1,2,3,4], ['a','b','c','d']):
...   print 'x', x, 'y', y
...
x 1 y a
x 2 y b
x 3 y c
x 4 y d

A little cumbersome, but it works. The map(...) expression returns
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')].

-- 
# - - - - - - - - - - - - - - - - - - - - - - - -
# Resistance is futile. Capacitance is efficacious.
# Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list