473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

elegant python style for loops

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
8 1310
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
17
by: Sunburned Surveyor | last post by:
I was thinking of a way I could make writing Python Class Files a little less painful. I was considering a Ptyhon script that read a file with a list of property names and method names and then...
0
by: Derek Martin | last post by:
On Fri, Jul 18, 2008 at 12:21:49PM -0700, mark floyd wrote: One wonders why... :) Even if Python didn't offer a way to write a for loop in a similar fashion (someone else replied about...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.