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"]}]
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
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}]