I have a list of dictionaries and I have to update an entire dictionary based on a specific key-value pair
values = [
{
"value" : "AAA",
"rank" : 10,
"id" : 1
},
{
"value" : "BBB",
"rank" : 50,
"id" : 2
}
]
I will receive a new list value_new = [{"value" : "CCC","rank" : 20,"id" : 1}]
. The idea is to match the id and update the entire dictionary related to that id and sort the entire values list based on rank.
I iterated through values list and matched based on id and when I tried to update it replaced the entire value list
I tried this piece of code
for val in range(len(values)):
if values[val]['id'] == value_new['id']:
values[val] = value_new
I am unable to sort it after appending.