[Tutor] advice: making dictionary from two lists?

Lance E Sloan lsloan@umich.edu
Wed, 16 May 2001 14:38:10 -0400


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"