I have a list of dictionaries and would like to update the value for key \'price\' with 0 if key price value is equal to \'\'
data=[a[\'price\']=0 for a in
It is bad practice, but possible:
import operator
l = [
{'price': '', 'name': 'Banana'},
{'price': 0.59, 'name': 'Apple'},
{'name': 'Cookie', 'status': 'unavailable'}
]
[operator.setitem(p, "price", 0) for p in l if "price" in p and not p["price"]]
The cases in which there's no key "price" is handled and price is set to 0 if p["price"] is False, an empty string or any other value that python considers as False.
Note that the list comprehension returns garbage like [None].