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.
Jochen Ritzel’s answer is the right way to do this 99.9999% of the time:
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
ininstead ofhas_key(because it’s more readable, and faster, and not deprecated):However, it’s probably worth using an
ifstatement instead of a ternary expression, because the fact that you’re avoiding theexpensiveComputationis important, and you want it to be more visible:If you expect the default case to be rare, this is better:
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:
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:
Then, it’s just:
This has the side-effect of setting
d["the key"]toexpensiveComputation()before returning it to you, so a later call tod["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 usingdefaultdict:Then, again, it’s just:
This one is appropriate when you want to generate the value separately each time, and not keep it around for later.