'Updating two dictionary lists by keys

I have dictionary list which I wanted to update with the keys exsits. I tried like below but it didn't work. Can someone please advice

dic1 =   [{"valid": 0, "correct": "abc", "other": ["aaa"]}]
dic2 =  [ {"correct": "morning", "other":["negative"]}] 

dict1.update(dic2)

Expected output:

[{"valid": 0, "correct": "morning", "other": ["negative"]}]


Solution 1:[1]

dic1[0].update(dic2[0]) 
print(dic1)

Since you have only one element in the list .i.e. a dictionary.

#output
[{'correct': 'morning', 'other': ['negative'], 'valid': 0}]

Solution 2:[2]

def update(dic1, dic2):
    for i in dic1[0]:
        if i in dic2[0]:
            dic1[0][i] = dic2[0][i]
    return(dic1)

dic1 = update(dic1, dic2)
print(dic1)

try this

Solution 3:[3]

dic1 and dic2 are lists. You need to apply the update function on a dictionary. This should solve your problem:

dic1 =   {"valid": 0, "correct": "abc", "other": ["aaa"]}
dic2 =  {"correct": "morning", "other":["negative"]}
dic1.update(dic2)
print (dic1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Talha Tayyab
Solution 2 White_Sirilo
Solution 3 peru_45