M. Hassaan M. Hassaan - 1 year ago 72
Python Question

How can I convert a list of lists into a dictionary of lists in Python?

I have a list with four lists in it. I want to assign each inner list a name, basically convert the list with four lists into a dictionary with four named lists. How can I do that in a Pythonic way? I saw this question but it was too specfic.

dictionary["list1"] = lists[0]
dictionary["list2"] = lists[1]
dictionary["list3"] = lists[2]
dictionary["list4"] = lists[3]

Edit: I would like to know how to do that with arbitrary dictionary keys - not just list1,list2,list3, etc.

Answer Source

This should do the trick

import itertools

names = "mains_voltage mains_time CT_voltage CT_time".split()
answer = {name:L for name,L in itertools.izip(names, lists)}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download