elegant python style for loops

I

ian.team.python

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?
 
D

Diez B. Roggisch

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?

for a, b in zip(lista, listb):
...

Diez
 
P

Peter Otten

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?

zip() creates a list of tuples, or better, itertools.izip() lazily creates
tuples as you go.

Peter
 
G

Gary Herron

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
 
A

Asun Friere

for a, b in zip(lista, listb):
...

You don't even need the for loop nowadays. Just pass the zipped list
to a dictionary constructor thusly:
newdict = dict(zip(listKeys,listValues))

Asun
 
I

ian.team.python

thank you everybody....very well answered.....just one question
remains....
where do i find documentation on zip ...i was looking for a function
like this, but could not even find a relevant list of functions!!
 
I

ian.team.python

thank you everybody....very well answered.....just one question
remains....
where do i find documentation on zip ...i was looking for a function
like this, but could not even find a relevant list of functions!!

ooops...even that was answered. again, thanks
 
A

Ant

On May 10, 6:51 am, (e-mail address removed) wrote:
....
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?

For the specific case of indexing lists, the following is cleaner than
the 'for i in range...' solution above, and works in cases where
zipping the lists may not be appropriate:

for i, item in enumerate(mylist):
print "%s) My item: %s; My other item: %s" % (i, item,
my_non_iterable_object.thing_at(i))
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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

newdict = dict(zip(listKeys, listValues))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top