use python list comprehension to update dictionary value

后端 未结 4 2114
我在风中等你
我在风中等你 2021-01-02 00:26

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          


        
4条回答
  •  悲哀的现实
    2021-01-02 00:33

    if you're using dict.update don't assign it to the original variable since it returns None

    [a.update(price=0) for a in data if a['price']=='']
    

    without assignment will update the list...

提交回复
热议问题