How I can get rid of None values in dictionary?

Solution 1:

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}

Solution 2:

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}