0

Is there a way in python to turn a set of lists into a dictionary, where the name of the list is the key and the values are the values? Can you do this with dictionary comprehension?

one = ['a', 'b', 'c']
two = ['d','e','f']

would become

dictionary = {"one" : ['a', 'b', 'c'], "two":['d','e','f'] }
SuperShoot
  • 9,880
  • 2
  • 38
  • 55
J Go
  • 21
  • 1
  • 3
  • 1
    a set of lists? like the type `set`?..... – jacoblaw Jul 20 '17 at 23:36
  • You'd have to show how these are populated. Is 'one' the name of a variable? An argument to a function? – a p Jul 20 '17 at 23:38
  • 2
    "one" is not the "name of the list." List objects have no such concept. "one" is the name of a Python variable bound to the list, but you could equally well have assigned to a variable named "three" and it would still be the same list. – Paul Cornelius Jul 20 '17 at 23:39
  • 1
    Possible duplicate of [Python - dictionary of lists](https://stackoverflow.com/questions/27770836/python-dictionary-of-lists) – victor Jul 20 '17 at 23:41
  • 1
    `dict(zip(["one", "two"], [one, two]))` – Paul Rooney Jul 20 '17 at 23:44
  • You can basically wrap the function `dict` around what you typed: dict(one=['a', 'b', 'c'], two = ['d','e','f']) – Mark Jul 20 '17 at 23:49

2 Answers2

0
>>> one = ['a', 'b', 'c']
>>> two = ['d','e','f']
>>> c = dict({'one':one, 'two':two})

or

>>> dict(zip(["one", "two"], [one, two]))

Why you want to convert one variable to 'one' and two variable to 'two' is incomprehensible.

Enigma
  • 329
  • 1
  • 10
0

I don't think you can necessarily access the variable name, but lets say, if you had a list of those lists, you could set the index of each one as the key value. example:

megaList = [[2, 3, 6], [3, 6, 1]]
dic = {}
for i, l in enumerate(megaList):
    dic[i] = l

but in that case you might as well just have the list of lists. Just though I'd share since I'm not sure what you're trying to do, and maybe this can steer you in the right direction.

Or do what @enigma did, if you don't mind typing them out by hand.

Mauricio
  • 419
  • 4
  • 14