'Ternary operation on dictionary
Is there a way to do a ternary operation on a dict where the test is has_key() without hitting the key error? i.e.
variable = dict["the_key"] if dict.has_key("the_key") else "something"
(this hits the key error obviously)
failing that, what's the most pythonic way to assign a bunch of variables values based on a dict that may or may not have the keys in question? Repeating:
if dict.has_key('key'):
value = dict['key']
else:
value = "value"
ad nauseum seems to be very inelegant.
Solution 1:[1]
Jochen Ritzel's answer is the right way to do this 99.9999% of the time:
variable = d.get("the key", "something")
As he notes, it doesn't allow you to short-circuit the evaluation of the default value. You usually don't care. It the default value is "something", you certainly don't. It only matters if the default is either dangerous, or very expensive, to generate.
Only in that case, you can and should use your idea. Except that you want in instead of has_key (because it's more readable, and faster, and not deprecated):
variable = d["the key"] if "the key" in d else expensiveComputation()
However, it's probably worth using an if statement instead of a ternary expression, because the fact that you're avoiding the expensiveComputation is important, and you want it to be more visible:
if "the_key" in d:
variable = d["the key"]
else:
variable = expensiveComputation()
If you expect the default case to be rare, this is better:
try:
variable = d["the key"]
except KeyError:
variable = expensiveComputation()
If for some reason you need to avoid looking the key up twice, and you also can't deal with exceptions, and you need to short-circuit the default, all at once:
sentinel = object()
variable = d.get(sentinel)
if variable == sentinel:
variable = expensiveComputation()
And yes, you could wrap that all up in a function so it's a one-liner, but you almost certainly don't want to hide the fact that you're doing three rare things all at once.
Or, of course, you could just make do this:
d = collections.defaultdict(expensiveComputation)
Then, it's just:
variable = d["the key"]
This has the side-effect of setting d["the key"] to expensiveComputation() before returning it to you, so a later call to d["the key"] will return the same value. If that sounds appropriate, this is the best answer; if you're never going to use the same key twice, or these things are huge and wasteful to keep around, etc., this is a bad idea.
Or, alternatively, you can override the dict.__missing__ method instead of using defaultdict:
class MyDict(dict):
def __missing__(self, key):
return expensiveComputation()
Then, again, it's just:
variable = d["the key"]
This one is appropriate when you want to generate the value separately each time, and not keep it around for later.
Solution 2:[2]
This:
mydict.get(key, default_value)
Not 100% the same as default_value is evaluated straight away while the else part is only evaluated when the condition is met.
Solution 3:[3]
Use the .get() method - it'll return None if the key doesn't exist, or you can set your own default:
http://docs.python.org/2/library/stdtypes.html#dict.get
value = mydict.get("key", "default")
Solution 4:[4]
variable = d['the_key'] if 'the_key' in d else 'something'
Solution 5:[5]
value = 'value'
if 'key' in dict:
value = dict['key']
or define a default value
dict.get('key', 'value')
Solution 6:[6]
If you're like me and have found this page because you're looking for a solution to the reverse of this problem, then this might help:
d.update({"the_key": variable} if variable else {})
So, rather than assign variable if "the_key" exists in d this is add "the_key" to d if variable exists.
Apologies if this isn't the right place for this answer, but I spent some time looking and couldn't find any questions explicitly asking for this when I was trying to solve it myself. This seemed the most appropriate place as the logic is the same, just in reverse.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jochen Ritzel |
| Solution 3 | Rachel Sanders |
| Solution 4 | Jo So |
| Solution 5 | jassinm |
| Solution 6 | clayajohnson |
