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.
Ternary Operator in Python. Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5.
Definition of ternary. 1a : of, relating to, or proceeding by threes. b : having three elements, parts, or divisions. c : arranged in threes ternary petals. 2 : using three as the base a ternary logarithm. 3a : being or consisting of an alloy of three elements.
We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. Although it follows the same algorithm as of if-else statement, the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
The above approach can be written as: Example: Find the Larger number among 2 using ternary operator in python3 First the given condition is evaluated (a < b), then either a or b is returned based on the Boolean value returned by the condition
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With