Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have two lists of dictionaries and a piece of code that tries to merge them:

Json_Final =[]

try:            

    for keyIN in JSON1:

        json_data_Merge= {}

        for keyUS in JSON2:                

            if(keyIN['YEAR'] == keyUS['YEAR']) & (keyIN['MONTH'] == keyUS['MONTH'])& (keyIN['Name'] == keyUS['Name']):

                json_data_Merge['YEAR'] = keyIN['YEAR']

                json_data_Merge['MONTH'] = keyIN['MONTH']

                json_data_Merge['Name'] = keyIN['Name']

                json_data_Merge['Total']= int(keyIN['Total']) + int(keyUS['Total'])

        Json_Final.append(json_data_Merge)

    print( Json_Final )

except Exception as e:

    print('MergeException',e)

JSON 1 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 100}, 

          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 200}, 

          {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300}, 

          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  100}, 

          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  200}]

JSON 2 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 200}, 

          {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300}, 

          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 400}, 

          {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300}, 

          {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200}, 

          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  500}, 

          {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200}, 

          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  250}]

Expected Output:

[{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 300}, 

 {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300}, 

 {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 600},

 {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300}, 

 {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300}, 

 {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200}, 

 {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  600}, 

 {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200}, 

 {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  450}]

My Code Output:

[{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 300}, 

{"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 600}, 

{"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  300}, 

{"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  450}]

1 Answer

0 votes
by (25.1k points)

You can do it like this:

JSON_1 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 100},

          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 200},

          {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300},

          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  100},

          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  200}]

 

JSON_2 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 200},

          {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300},

          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 400},

          {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300},

          {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200},

          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  500},

          {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200},

          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  250}]

 

JSON_2 = {"{}_{}_{}".format(i["YEAR"], i["MONTH"], i["Name"]): i for i in JSON_2}    #Create a dict for easy loopup

 

for i in JSON_1:

key = "{}_{}_{}".format(i["YEAR"], i["MONTH"], i["Name"])   

    if key in JSON_2:                     

    JSON_2[key]['Total'] += i["Total"]    

    else:

    JSON_2[key] = i                 

print(list(JSON_2.values()))    #Get values.

Related questions

+2 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...