Carl H Carl H - 5 months ago 9
Python Question

How to get dictionary values from list of lists (any language)?

I most interested in simplicity, so would appreciate solution in any language (also interesting how it would look using LINQ). I tried to do it in Python, but failed.

From these two lists:

init_li1 = [[234,45,1,86,2,0],[324,6,1],[123,1111,3]]
init_li2 = ["Alpha", "Beta", "Gamma"]

I would like to get this dictionary:

{"Alpha":[234,45,1,86,2,0], "Beta":[324,6,1], "Gamma":[123,1111,3]}


Actually, it's very easy in Python:

dictionary = dict(zip(init_li2, init_li1))

You see, the dict constructor can be used like this:

dict(('key1', 'value1'), ('key2', 'value2'))

Well, zip() will generate tuples by taking corresponding elements from its arguments. That is:

zip([4, 5, 6], [2, 3, 4])
#-> (4, 2), (5, 3), (6, 4)

Therefore, we take the keys from init_li2, and the values from init_li1.