[Tutor] advice: making dictionary from two lists?

Benoit Dupire bdupire@seatech.fau.edu
Wed, 16 May 2001 15:42:28 -0400


Roeland Rengelink wrote:

> from UserDict import UserDict
>
> class genie_dict(UserDict):
>     def inverse_of_items(self, item_list):
>         for key, value in item_list:
>             self.data[key] = value

Nice!!!!!!!!
so, using your idea:

class superDict(UserDict):
    def __init__(self, list1=[], list2=[]):
        self.data={}
        foo =map(operator.setitem, [self.data]*len(list1), list1, list2)

not tested, but that should work....

I was interested to see what the result is, if list2 is shorter than list1

>>> labels = ('name', 'age', 'salary')
>>>values = ('Monty', 42)
>>> map(operator.setitem, [d]*len(labels), labels, values)

Result...

>>> d
{'age': 42, 'name': 'Monty', 'salary': None}


Ouaaaa!!!! Nice !!!!


Benoit