If you have a lot of variables and donβt want to hard code the dictionary understanding, here is the way.
NOTE: you need to declare all variables.
You also need to declare a list of variable names.
list_of_var_names = ['triangles', 'circles', 'squares'] dict(zip(index, [dict(zip(list_of_var_names, i)) for i in (globals().get(i) for i in list_of_var_names)]))
And split step by step:
In [1]: index = [1,2,3] ...: ...: triangles = [4,5,6] ...: circles = [7,8,9] ...: squares = [10,11,12] ...: In [2]: list_of_var_names = ['triangles', 'circles', 'squares'] In [3]: [globals().get(i) for i in list_of_var_names] # getting list of variable values in list_of_var_names order Out[3]: [[4, 5, 6], [7, 8, 9], [10, 11, 12]] In [4]: [dict(zip(list_of_var_names, i)) for i in (globals().get(i) for i in lis ...: t_of_var_names)] Out[4]: [{'circles': 5, 'squares': 6, 'triangles': 4}, {'circles': 8, 'squares': 9, 'triangles': 7}, {'circles': 11, 'squares': 12, 'triangles': 10}] In [5]: dict(zip(index, [dict(zip(list_of_var_names, i)) ...: for i in (globals().get(i) for i in list_of_var_names)] ...: )) ...: Out[5]: {1: {'circles': 5, 'squares': 6, 'triangles': 4}, 2: {'circles': 8, 'squares': 9, 'triangles': 7}, 3: {'circles': 11, 'squares': 12, 'triangles': 10}}
I want to mention once again that this solution, if it is good, if you get a lot of variables, and you do not want to explicitly declare your understanding of the dict. In other cases, it would be more appropriate and more readable to use the other solutions presented here.