[Tutor] advice: making dictionary from two lists?
Benoit Dupire
bdupire@seatech.fau.edu
Wed, 16 May 2001 15:12:16 -0400
in the Python doc at
http://www.python.org/doc/current/lib/module-operator.html
there is a similar problem...
For your problem, it will give
import operator
d = {}
labels = ('name', 'age', 'salary')
values = ('Monty', 42, 5)
map(operator.setitem, [d]*len(labels), labels, values)
>>>d
{'age': 42, 'name': 'Monty', 'salary': 5}
___________________
setitem(a, b, c)
__setitem__(a, b, c)
Set the value of a at index b to c.
______________________
I don't know if it is better....
Benoit
Lance E Sloan wrote:
> I've got a couple lists, one is of labels and the other is values.
> They have a one-to-one correspondence. Currently, I'm using this
> method to make them into a dictionary:
>
> labels = ('name', 'age', 'salary')
> values = ('Monty', 42, 5)
>
> # make a dictionary from two lists/tuples
> theDict = {} # or whatever you want to call it
> for (key, value) in map(None, labels, values):
> theDict[key] = value
>
> This works fine, but I just wondered if there was a better (or
> "cooler") way to do this. I wish that dictionaries had an inverse of
> the items() method.
>
> --
> Lance E Sloan
> Web Services, Univ. of Michigan: Full-service Web and database design,
> development, and hosting. Specializing in Perl & Python CGIs.
> http://websvcs.itd.umich.edu/ - "Putting U on the Web"
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?