Accessing Value Inside Python Nested Dictionaries
We are given a nested dictionary and our task is to access the value inside the nested dictionaries in Python using different approaches. In this article, we will see how we can access value inside the nested dictionaries in Python.
Example:
Input: market = {"fruits": {"apple": 10, "orange": 40, "strawberry": 30, "grapes": 200},
"vegetables": {"carrot": 50, "beans": 100, "tomato": 70}}
Output: Orange Price: 40
Explanation: Orange price is accessed from the given nested dictionary and printed.
Access Value Inside Python Nested Dictionaries
Below are some of the ways by which we can access the value inside the Python Nested Dictionaries in Python:
- Using Indexing Method
- Using get() method
- Using Recursion
Access Value Inside Python Nested Dictionaries Using Square brackets
In this example, values in the nested market
dictionary are accessed using square brackets and specific keys. The prices of orange, grapes, and tomatoes are printed by navigating through the nested structure with the respective keys for fruits and vegetables.
market = {"fruits": {"apple": 10, "orange": 40, "strawberry": 30, "grapes": 200},
"vegetables": {"carrot": 50, "beans": 100, "tomato": 70}}
# printing the value by accessing using square brackets with key of dictionary
print("Orange Price:",market["fruits"]["orange"])
print("Grapes Price:",market["fruits"]["grapes"])
print("Tomato Price:",market["vegetables"]["tomato"])
Output
Orange Price: 40 Grapes Price: 200 Tomato Price: 70
Access Value Inside Python Nested Dictionaries Using get() Method
In this example, values in the nested market
dictionary are accessed using the get()
method. The prices of orange, grapes, and tomatoes are printed by chaining get()
calls with the respective keys for fruits and vegetables, providing a safe way to handle potential missing keys.
market = {"fruits": {"apple": 10, "orange": 40, "strawberry": 30, "grapes": 200},
"vegetables": {"carrot": 50, "beans": 100, "tomato": 70}}
# Printing the value using get() method of dictionary by passing key
print("Orange Price:", market.get("fruits").get("orange"))
print("Grapes Price:", market.get("fruits").get("grapes"))
print("Tomato Price:", market.get("vegetables").get("tomato"))
Output
Orange Price: 40 Grapes Price: 200 Tomato Price: 70
Access Value Inside Python Nested Dictionaries Using Recursive Function
In this example, a recursive function named get_nested_value
is used to retrieve the quantity of beans from the nested market
dictionary. The function recursively navigates through the dictionary hierarchy, utilizing the get()
method to handle missing keys, and prints the quantity of beans as the output.
# Using a recursive function
market = {"fruits": {"apple": 10, "orange": 40, "strawberry": 30, "grapes": 200},
"vegetables": {"carrot": 50, "beans": 100, "tomato": 70}}
def get_nested_value(dictionary, keys):
if not keys:
return dictionary
return get_nested_value(dictionary.get(keys[0], {}), keys[1:])
# Example usage
beans_quantity = get_nested_value(market, ['vegetables', 'beans'])
print("Beans Quantity:", beans_quantity) # Output: 100
Output
Beans Quantity: 100