Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

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?

ian.team.pyt@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

ian.team.pyt@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

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

On May 10, 4:20 pm, "Diez B. Roggisch" <d@nospam.web.de> wrote:

> 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

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!!
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
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.

ian.team.pyt@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))
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc