adding list element to dictionary

Viewed 27

I have a list like this

mylist = ['a','d', 'e']

and a dic

mydic = {'ASWD': ['13', '14', '15'], 'DDW': ['6', '7', '8', '9']}

I want to loop through this mydic and add mylist item with creating new dictionary

my_newest_dic = {'datasets': []}

for k, v in mydic.items():
    for c in range(len(mylist)):

        temp = {
                        'hello':k,
                        'world': mylist[c],
                        'labels' : v    
        }

        my_newest_dic['datasets'].append(temp)

print(my_newest_dic)

but I got

>{'datasets': [{'hello': 'ASWD', 'world': 'a', 'labels': ['13', '14', '15']}, {'hello': 'ASWD', 'world': 'd', 'labels': ['13', '14', '15']}, {'hello': 'ASWD', 'world': 'e', 'labels': ['13', '14', '15']}, {'hello': 'DDW', 'world': 'a', 'labels': ['6', '7', '8', '9']}, {'hello': 'DDW', 'world': 'd', 'labels': ['6', '7', '8', '9']}, {'hello': 'DDW', 'world': 'e', 'labels': ['6', '7', '8', '9']}]}

The expected output

my_newest_dic

[{'hello': 'ASWD', 'world': 'a', 'labels': ['13', '14', '15']}, {'hello': 'DDW', 'world': 'd', 'labels': ['6', '7', '8','9']}]
0 Answers
Related