parallel for loops

Paramjit Oberoi p_s_oberoi at hotmail.com
Tue Apr 27 19:51:28 EDT 2004


>>>> a, b = [1,2,3], [4,5,6]     # a = [1,2,3], b = [4,5,6]
>>>> for a, b in zip([1,2,3], [4,5,6]): print a, b
> 
> instead of:
>>>> for a, b in [1,2,3], [4,5,6]: print a, b     # illegal
> 
> im sure there is a good reason why the former was chosen, and i know its way
> too late to switch, but i cant think of many examples of when you would use
> parallel iteration *without* zip. not even dictionaries since you have to

dates = [(2004, 4, 27), (2004, 2, 9), (2003, 11, 14)]
for year, month, day in dates:
    do_something_with(year)
    and_with(month, date)

Also, it's more consistent: in each iteration, an element of the
list being iterated over is assigned to the loop variable; or if
it's multiple variables, automatic unpacking happens just like it
would if a value were assigned to multiple variables in an
assignment statement.



More information about the Python-list mailing list