Reputation: 13642
In this scenario
>>> x = {}
>>> x.get('test') #Prints None
>>> x.get('test','') #Prints empty string
''
>>> x = {'test':None}
>>> x.get('test') #Prints None
>>> x.get('test','') #Prints None
How do I get empty string printed in both the cases by treating None valued key as non-existant ?
Upvotes: 1
Views: 41
Reputation: 60984
Wrap the get
call in a function that checks if the value is None
def get_except_None(d, key):
val = d.get(key, '')
if val is None:
return ''
return val
So instead of x.get('test', '')
in your code, you would do get_except_None(x, test)
Upvotes: 3