Something like:
for (a,b) in kwargs.iteritems(): if not b : del kwargs[a]
This code raise exception because changing of dictionary when iterating.
I discover only non pretty solution with another dictionary:
res ={} res.update((a,b) for a,b in kwargs.iteritems() if b is not None)
Thanks
In Python, the clear() method is used to delete a dictionary Python. This method removes all the keys values pairs that are available in the dictionary and always returns a none value or empty dictionary.
The Python del statement deletes an object. Because key-value pairs in dictionaries are objects, you can delete them using the “del” keyword. The “del” keyword is used to delete a key that does exist. It raises a KeyError if a key is not present in a dictionary.
The dictionary keys and values can be of any type. They can also be None .
Another way to write it is
res = dict((k,v) for k,v in kwargs.iteritems() if v is not None)
In Python3, this becomes
res = {k:v for k,v in kwargs.items() if v is not None}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With