David Gardner
David Gardner

Reputation: 149

How to generate a nested dictionary?

I wanted to create a nested dictionary, like {'match':{'sam':65}, 'match':{'john':55}}. When I run my code it generate the final dictionary as {'match':{'sam':55},{'john':65}}. How should I create a nested dictionary as discussed above?

following is my code:

        attribute = ['sam','john']
        value = [65,55]
        dict = {}
        main_dict = {}

        for j in range(len(attribute)):
            dict[attribute[j]] = value[j]

        for key, value in dict.items():
            main_dict['match'][key] = dict[key]

Upvotes: 0

Views: 51

Answers (1)

heydar dasoomi
heydar dasoomi

Reputation: 557

Cannot create dictionary with two same key.

Upvotes: 1

Related Questions