I have a dict with some value in it. Now at the time of fetching value I want to check if value is None
replace it with ""
. Is there any single statement way for this.
a = {'x' : 1, 'y': None}
x = a.get('x', 3) # if x is not present init x with 3
z = a.get('z', 1) # Default value of z to 1
y = a.get('y', 1) # It will initialize y with None
# current solution I'm checking
if y is None:
y = ""
What I want is something single line (pythonic way). I can do this in below way
# This is one way I can write but
y = "" if a.get('y',"") is None else a.get('y', "")
But from what I know there must be some better way of doing this. Any help