David Hancock
David Hancock

Reputation: 473

How to make a dictionary of dictionaries with multiple lists

There is one index list which will be the key of the parent dictionary:

index = [1,2,3]

and then multiple lists of which will be the child dicts:

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]

the sequential elements being the data, resulting in:

{1:{'triangles':4, 'circles':7, 'squares': 10},
 2: {'triangles': 5, 'circles': 8, 'squares': 11},
 3: {'triangles': 6, 'circles': 9, 'squares': 12}}

how can I do this ?

Do you think easier to do in pandas ?

Upvotes: 6

Views: 248

Answers (7)

Moses Koledoye
Moses Koledoye

Reputation: 78554

You can zip the lists, create the subdicts and then zip the subdicts with the indices. No restrictions on the indices; they can be non-sequencial/non-numerical:

dct =  dict(zip(index, ({'triangles': i, 'circles': j, 'squares': k} 
                          for i,j,k in zip(triangles, circles, squares))))
print(dct)

{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

On another note, if you only need sequential counts, the index list can be replaced with enumerate:

dct =  dict(enumerate(({'triangles': i, 'circles': j, 'squares': k} 
                          for i,j,k in zip(triangles, circles, squares)), 1))

Upvotes: 4

Chiheb Nexus
Chiheb Nexus

Reputation: 9267

Another variety using double zip() and dict comprehension:

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]
index = [1,2,3]

b = {k:{'triangles': x, 'circles': y, 'squares': z} for k, (x,y,z) in zip(
                                  index, zip(triangles, circles, squares))}
print(b)

Output:

{1: {'circles': 7, 'squares': 10, 'triangles': 4}, 
2: {'circles': 8, 'squares': 11, 'triangles': 5}, 
3: {'circles': 9, 'squares': 12, 'triangles': 6}}

Upvotes: 0

vishes_shell
vishes_shell

Reputation: 23554

If you got a lot of variables and you don't want to hardcode your dictionary comprehension, here is a way.

NOTE: you need to have all variables declared.

Also you need to declare 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 to divide 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 one more time that this solution if good if you get a ton of variables and you don't want explicitly declare dict comprehension. In other cases it would be more suitable and more readable to use other solutions that are presented here.

Upvotes: 0

zaidfazil
zaidfazil

Reputation: 9245

You could do something like this,

results = {}
for index, item in enumerate(zip(triangles,circles,squares)):
    results.update({index+1:{'triangles':item[0], 'circles':item[1], 'squares':item[2]}})

Out[6]: 
{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

Upvotes: 0

akash karothiya
akash karothiya

Reputation: 5950

The simplest method is with dict comprehension :

>>> d = {i:{'triangles':triangles[i-1],'circles':circles[i-1],'squares':squares[i-1]} for i in index}

{1: {'circles': 7, 'squares': 10, 'triangles': 4},
 2: {'circles': 8, 'squares': 11, 'triangles': 5},
 3: {'circles': 9, 'squares': 12, 'triangles': 6}}

Upvotes: 1

Mureinik
Mureinik

Reputation: 312219

Dict comprehnesions to the rescue!
Note, BTW, that the indices stored in index seem to be one-based although python lists are zero-based:

result =  {i : {'triangles' : triangles[i-1], 'circles' : circles[i-1], 'squares' : squares[i-1]} for i in index}

Upvotes: 1

moritzg
moritzg

Reputation: 4394

Actually this is very easy and can be achieved with a simple for-loop:

index = [1,2,3]

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]

dictionary = {}

for i in range(0, len(index)):
    dictionary[index[i]] = {'triangles':triangles[i], 'circles':circles[i], 'squares':squares[i]}

print(dictionary)

Output:

{1: {'triangles': 4, 'circles': 7, 'squares': 10}, 2: {'triangles': 5, 'circles': 8, 'squares': 11}, 3: {'triangles': 6, 'circles': 9, 'squares': 12}}

Upvotes: 0

Related Questions