What is the most pythonic way to repeat an expression in Python.
Context : A function is getting passed a dictionary (with possibly 20 elements) and I need to extract and use values from this dictionary if they exist. Something like this :
x = dict_name['key1'] if dict_name['key1'] else ''
Now, instead of writing this logic 20 times for all 20 different keys, I would like to define this expression at one place and then use it repeatedly. It will be easier to maintain in future. In "C/C++" programming languages, it makes a good case of #define
or inline
functions. What would be Pythonic way to do this in Python?
As you would have guessed, I am novice in Python. So I appreciate your help on this or pointers to constructs for this in Python.