I have a very simple use case in which I have to update a key inside a list of dictionaries. The code goes like this:
original = [
{
"name": "Subhayan",
"age": 34
},
{
"name": "Poulomi",
"age": 30
}
]
update_data = {
"Subhayan": 50,
"Poulomi": 46
}
check = [x["age"] = update_data[x["name"]] for x in original]
print(check)
I have this strange error :
File "try_defaultdict.py", line 17
check = [x["age"] = update_data[x["name"]] for x in original]
^
SyntaxError: invalid syntax
I know this can be done using a simple for
loop. But I am wondering if I can use the list comprehension method for doing this?