Python | Safe access nested dictionary keys
Sometimes, while working with Python we can have a problem in which we need to get the 2nd degree key of dictionary i.e the nested key. This type of problem is common in case of web development, especially with the advent of NoSQL databases. Let’s discuss certain ways to safely get the nested available key in dictionary.
Method #1 : Using nested get()
This method is used to solve this particular problem, we just take advantage of the functionality of get() to check and assign in absence of value to achieve this particular task. Just returns non-error None if any key is not present.
# Python3 code to demonstrate working of # Safe access nested dictionary key # Using nested get() # initializing dictionary test_dict = {'Gfg' : {'is' : 'best'}} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # using nested get() # Safe access nested dictionary key res = test_dict.get('Gfg', {}).get('is') # printing result print("The nested safely accessed value is : " + str(res)) |
The original dictionary is : {'Gfg': {'is': 'best'}}
The nested safely accessed value is : best
Method #2 : Using reduce() + lambda
The combination of above functions can be used to perform this particular task. It just checks using lambda function for the available keys. The plus of this method is that more than 1 key can be queried at once i.e more nested level keys, but the negative is that it does only work with Python2.
# Python code to demonstrate working of # Safe access nested dictionary key # Using reduce() + lambda # initializing dictionary test_dict = {'Gfg' : {'is' : 'best'}} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # using reduce() + lambda # Safe access nested dictionary key keys = ['Gfg', 'is'] res = reduce(lambda val, key: val.get(key) if val else None, keys, test_dict) # printing result print("The nested safely accessed value is : " + str(res)) |
The original dictionary is : {'Gfg': {'is': 'best'}}
The nested safely accessed value is : best
Recommended Posts:
- Python | Add keys to nested dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Nested Dictionary
- Python | Sum values for each key in nested dictionary
- Python | Sort nested dictionary by key
- Python | Add new keys to a dictionary
- Python | Get the number of keys with given value N in dictionary
- Python | Minimum value keys in Dictionary
- Python | Get total keys in dictionary
- Python | Count keys with particular value in dictionary
- Python | Get dictionary keys as a list
- Python | Get all tuple keys from dictionary
- Python | Grouping dictionary keys by value
- Python Dictionary | keys() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


