Search code examples
pythondictionaryconditional-operator

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

  • 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.