the.bay.news

How can I preserve 0 and False while replacing only None values in a Python dictionary?

Discussions on Python.org
How can I preserve 0 and False while replacing only None values in a Python dictionary?
I am building a dictionary from optional keyword arguments. Currently I have code like this: payload = { "premium": kwargs.get("premium") or "", "comments": kwargs.get("comments") or "", "overridden": kwargs.get("overridden", False), } The problem is that using or "" also replaces valid falsy values. For example: kwargs = { "premium": 0, "comments": None, "overridden": False, } I want the resulting dictionary to be: { "premium": 0, "comments": "", "over...

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.