elegant python style for loops
Gary Herron
gherron at islandtraining.com
Thu May 10 02:24:11 EDT 2007
ian.team.python at saltmob.com wrote:
> To step through a list, the python style is avoid an explicit index.
> But what if the same hidden index is to be used for more than one list
>
> for example:-
> for key,value in listKeys,listValues :
> newdict[key]=value
>
> won't work as it is a tuple of lists, as opposed to a list of tuples.
> Is there an elegant solution to this? Is there a way to merge lists
> into a list of tuples to allow moving through multiple lists, or is
> the for i in range(len(listkeys)): the only solution?
>
> Any suggestions?
>
>
Yes. The builtin function zip does just that: merging separate lists
into a list of tuples.
See: http://docs.python.org/lib/built-in-funcs.html#l2h-81
Gary Herron
More information about the Python-list
mailing list