Home > Mobile >  Create a nested dictionary from 3 lists and 1 key with for loop in Python
Create a nested dictionary from 3 lists and 1 key with for loop in Python

Time:03-30

I was trying to create a nested dictionary with a for loop. But I have got a problem with the key in the loop. I have got these 6 lists (these two are a sample from my 500 boardgames data):

 [['Marco Polo Ii'] 
 ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics', 'Number of Reviews']
 ['7.3', '6.6', '7.9', '7.3', '6', '8.5', '7.1']]
 [['Its A Wonderful Kingdom'] 
 ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics'] 
 ['7.8', '7.7', '6.1', '9.5', '6', '6.8']]

This is what I am expecting to have:

 d={
 'Marco Polo Ii'{'Total':7.3, 'Components':6.6, 'Fun':'7.9, 'Replayability':7.3, 'Originality':6, 'Mechanics':8.5, 'Number of Reviews':7.1},
 'Its A Wonderful Kingdom'{'Total':7.8, 'Components':7.7, 'Fun':'6.1, 'Replayability':9.5, 'Originality':6, 'Mechanics':6.8}}

This is what I get in the print:

{Marco Polo Ii'{'Total':7.3},'Its A Wonderful Kingdom'{'Total':7.8}}

This is the code I tried:

d={}
key=[]   
category=[]
value=[]
d = {k: {str(c): str(v)} for k, c, v in zip(key,category,value)}

# This for loop I searched is used when there is the same number of keys, categories, and values.

I also tried this:

for k in key:
   for c,v in zip(category,value)[k]:
       d[k][c]=v

But an error occurred.

Thank you for any help!

CodePudding user response:

Iterating over each game list of lists in a dict comprehension:

>>> games = [[['Marco Polo Ii'],
...  ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics', 'Number of Reviews'],
...  ['7.3', '6.6', '7.9', '7.3', '6', '8.5', '7.1']],
...  [['Its A Wonderful Kingdom'] ,
...  ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics'] ,
...  ['7.8', '7.7', '6.1', '9.5', '6', '6.8']]]
>>> result = {l[0][0]: { l[1][k]:l[2][k] for k in range(len(l[1])) } for l in games }
>>> print(json.dumps(result, indent=4))
{
    "Marco Polo Ii": {
        "Total": "7.3",
        "Components": "6.6",
        "Fun": "7.9",
        "Replayability": "7.3",
        "Originality": "6",
        "Mechanics": "8.5",
        "Number of Reviews": "7.1"
    },
    "Its A Wonderful Kingdom": {
        "Total": "7.8",
        "Components": "7.7",
        "Fun": "6.1",
        "Replayability": "9.5",
        "Originality": "6",
        "Mechanics": "6.8"
    }
}
>>>

CodePudding user response:

I think this code snippet helps.

all_data = [[['Marco Polo Ii'],
 ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics', 'Number of Reviews'],
 ['7.3', '6.6', '7.9', '7.3', '6', '8.5', '7.1']],

 [['Its A Wonderful Kingdom'],
 ['Total', 'Components', 'Fun', 'Replayability', 'Originality', 'Mechanics'],
 ['7.8', '7.7', '6.1', '9.5', '6', '6.8']]]
 
data_dict ={}
for data in all_data:
    temp_dict = {}
    main_key = data[0][0]
    sub_key = data[1]
    sub_value = data[2]
    for index,key in enumerate(sub_key):
        temp_dict[key] = sub_value[index]

    data_dict[main_key] = temp_dict

print(data_dict.keys())   

for key in data_dict.keys():
    print(data_dict[key])
  • Related