TL DR
Is there a Python dictionary method or a simple expression that returns a value for the first key (from a list of possible keys) that exists in the dictionary?
More details
Let's say that I have a Python dictionary with a series of key-value pairs. The existence of a particular key is not guaranteed.
d = {'A':1, 'B':2, 'D':4}
If I want to get a value for a given key and return another default value (for example None), if this key does not exist, I just do:
my_value = d.get('C', None) # Returns None
But what if I want to check a few possible keys before you default to default? One of the methods:
my_value = d.get('C', d.get('E', d.get('B', None))) # Returns 2
but this becomes rather confusing as the number of alternative keys increases.
Python ? - :
d.get_from_first_key_that_exists(('C', 'E', 'B'), None)
, , ?