0

so I have 3 lists

a = [1 , 2 , 3]
b = [4 , 5 , 6]
c = [7 , 8 , 9]

I would like to zip them into a dictonary using the list name as key and the list itself as value.The output Im looking for is:

dictonary = { "a" : [1 , 2, 3] ,
              "b" : [4 , 5 , 6],
              "c" : [7, 8 , 9]}

Is this possible? thanks in advance

I tried:

dictonary = dict(zip(a, b, c))
print(dictonary)

but it ask me for two arguments instead of 3

Mark
  • 90,562
  • 7
  • 108
  • 148
  • Related: [How do I create variable variables?](/q/1373164/4518341), [Getting the name of a variable as a string](/q/18425225/4518341) – wjandrea Jul 05 '23 at 00:48
  • 2
    You could do `dictonary = {"a": a, "b": b, "c", c}`. Or if you wanted to be really tricky (although I do not recommend being tricky -- it's better to find a simpler solution, like just putting these lists in a dictionary in the first place instead of assigning them to variables and then trying to convert the variables into a dictionary), `dictonary = {var: globals()[var] for var in "abc"}`. – Samwise Jul 05 '23 at 01:11
  • [Can an object inspect the name of the variable it's been assigned to?](https://stackoverflow.com/q/8875202/674039) – wim Jul 05 '23 at 01:19

3 Answers3

1
a = [1 , 2 , 3]
b = [4 , 5 , 6]
c = [7 , 8 , 9]

d = {}
d['a'] = a
d['b'] = b
d['c'] = c

This can be a solution but I'm sure there are better solutions.

kbbq
  • 24
  • 2
0

If you're having this issue you might first want to question your choice of using variables for your data instead of placing it in a dictionary from the get go.

In order to get the dictionary you're looking for, there are several approaches (zip is but one of them):

d = dict(zip("abc",(a,b,c)))

d = dict(a=a, b=b, c=c)

d = {"a":a, "b":b, "c":c}

d = {v:eval(v) for v in "abc"}
Alain T.
  • 40,517
  • 4
  • 31
  • 51
-4
loc = locals()
def get_var_name(var):
    for k, v in loc.items():
        if loc[k] is var:
            return k

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# zip takes two iterable object of equal length as args
dictonary = dict(zip([get_var_name(t) for t in [a, b, c]], [a, b, c]))
print(dictonary)
Xiaomin Wu
  • 400
  • 1
  • 5
  • 2
    Since you need to type the variables twice in the source code, why would you do this rather than just putting quotes around the `a`, `b`, and `c` and making a regular dict? It would avoid looping through every local variable and be easier to read. Would this _ever_ be a good thing to recommend? The OP does not seem to be an experienced Python programmer, is this a good practice for new Python programmers to incorporate into their work? – Mark Jul 05 '23 at 02:00
  • critical is useless @Mark – Xiaomin Wu Jul 05 '23 at 02:55
  • This could produce different names in the dictionary if there happens to be another variable referencing the same list in locals(). Also, you could avoid repeating the list of variable names by using a comprehension instead of zip: `{get_var_name(t):t for t in [a,b,c]}` – Alain T. Jul 05 '23 at 22:36
  • @Aliain T, yes, but the origin question use zip, and I too – Xiaomin Wu Jul 06 '23 at 01:39
  • Thank you guys for your time and help! – Diego Sebastiao Jul 06 '23 at 22:23