>>> d2
{'egg': 3, 'ham': {'grill': 4, 'fry': 6, 'bake': 5}, 'spam': 2}
>>> d2.get('spamx',99)
99
>>> d2.get('ham')['fry']
6
I want to get value of fry inside of ham, if not, get value, 99 or 88 as the 2nd example. But how?
>>> d2
{'egg': 3, 'ham': {'grill': 4, 'fry': 6, 'bake': 5}, 'spam': 2}
>>> d2.get('spamx',99)
99
>>> d2.get('ham')['fry']
6
I want to get value of fry inside of ham, if not, get value, 99 or 88 as the 2nd example. But how?
I would probably break it down into several statements in real life.
If you need to do this a lot, you can write a helper function
For the default values of get to work correctly the first default needs to be a dictionary, so that you can chain the .get calls correctly if the first fails.
you could also use a try, except block
Here's a solution for dealing with nested dictionaries:
Usage: