How I can get rid of None values in dictionary?

后端 未结 7 915
无人共我
无人共我 2020-12-03 02:26

Something like:

for (a,b) in kwargs.iteritems():
    if not b : del kwargs[a]

This code raise exception because changing of dictionary when

7条回答
  •  攒了一身酷
    2020-12-03 03:14

    You can also use filter:

    d = dict(a = 1, b = None, c = 3)
    
    filtered = dict(filter(lambda item: item[1] is not None, d.items()))
    
    print(filtered)
    {'a': 1, 'c': 3}
    

提交回复
热议问题