Checking if a key exists and its value is not an empty string in a Python dictionary
Question:
Is there a clear best practice for assigning a variable from a key/value pair in a Python dictionary:
- If the key is present
- If the key’s value is not an empty string
And otherwise assigning a default value to the variable.
I would like to use dict.get
:
my_value = dict.get(key, my_default)
But this assigns an empty string to my_value
if the key is present and the value is an empty string. Is it better to use the following:
if key in dict and dict[key]:
my_value = dict[key]
else:
my_value = my_default
This would make use of the truthfulness of an empty string to ensure only non-empty strings were assigned to my_value
.
Is there a better way to perform this check?
Answers:
Maybe you mean something like:
a.get('foo',my_default) or my_default
which I think should be equivalent to the if-else
conditional you have
e.g.
>>> a = {'foo':''}
>>> a.get('foo','bar') or 'bar'
'bar'
>>> a['foo'] = 'baz'
>>> a.get('foo','bar') or 'bar'
'baz'
>>> a.get('qux','bar') or 'bar'
'bar'
The advantages to this over the other version are pretty clear. This is nice because you only need to perform the lookup once and because or
short circuits (As soon as it hits a True
like value, it returns it. If no True-like value is found, or
returns the second one).
If your default is a function, it could be called twice if you write it as: d.get('foo',func()) or func()
. In this case, you’re better off with a temporary variable to hold the return value of func
.
The simplest way to do what you want:
my_value = dict.get(key) or my_default
The or
will deliver the first value if it evaluates non-false, otherwise the second one. Unlike other languages Python doesn’t force the result to be boolean, quite a useful property sometimes.
this worked for me
try:
post_profile = record['ownerUsername']
except KeyError:
post_profile = "nouserwasfound"
Is there a clear best practice for assigning a variable from a key/value pair in a Python dictionary:
- If the key is present
- If the key’s value is not an empty string
And otherwise assigning a default value to the variable.
I would like to use dict.get
:
my_value = dict.get(key, my_default)
But this assigns an empty string to my_value
if the key is present and the value is an empty string. Is it better to use the following:
if key in dict and dict[key]:
my_value = dict[key]
else:
my_value = my_default
This would make use of the truthfulness of an empty string to ensure only non-empty strings were assigned to my_value
.
Is there a better way to perform this check?
Maybe you mean something like:
a.get('foo',my_default) or my_default
which I think should be equivalent to the if-else
conditional you have
e.g.
>>> a = {'foo':''}
>>> a.get('foo','bar') or 'bar'
'bar'
>>> a['foo'] = 'baz'
>>> a.get('foo','bar') or 'bar'
'baz'
>>> a.get('qux','bar') or 'bar'
'bar'
The advantages to this over the other version are pretty clear. This is nice because you only need to perform the lookup once and because or
short circuits (As soon as it hits a True
like value, it returns it. If no True-like value is found, or
returns the second one).
If your default is a function, it could be called twice if you write it as: d.get('foo',func()) or func()
. In this case, you’re better off with a temporary variable to hold the return value of func
.
The simplest way to do what you want:
my_value = dict.get(key) or my_default
The or
will deliver the first value if it evaluates non-false, otherwise the second one. Unlike other languages Python doesn’t force the result to be boolean, quite a useful property sometimes.
this worked for me
try:
post_profile = record['ownerUsername']
except KeyError:
post_profile = "nouserwasfound"