By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,629 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,629 IT Pros & Developers. It's quick & easy.

elegant python style for loops

P: n/a
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?

May 10 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ia*************@saltmob.com schrieb:
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

May 10 '07 #2

P: n/a
ia*************@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?
zip() creates a list of tuples, or better, itertools.izip() lazily creates
tuples as you go.

Peter
May 10 '07 #3

P: n/a
ia*************@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
May 10 '07 #4

P: n/a
On May 10, 4:20 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
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

May 10 '07 #5

P: n/a
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!!
May 10 '07 #6

P: n/a
On May 10, 6:00 pm, ian.team.pyt...@saltmob.com wrote:
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

May 10 '07 #7

P: n/a
Ant
On May 10, 6:51 am, ian.team.pyt...@saltmob.com 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))

--
Ant.

May 10 '07 #8

P: n/a
ia*************@saltmob.com 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))
May 10 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.