Python – Extract values of Particular Key in Nested Values
Last Updated :
28 Apr, 2023
Given a dictionary with nested dictionaries as values, extract all the values with of particular key.
Input : test_dict = {‘Gfg’ : {“a” : 7, “b” : 9, “c” : 12}, ‘is’ : {“a” : 15, “b” : 19, “c” : 20}, ‘best’ :{“a” : 5, “b” : 10, “c” : 2}}, temp = “b”
Output : [9, 10, 19]
Explanation : All values of “b” key are extracted.
Input : test_dict = {‘Gfg’ : {“a” : 7, “b” : 9, “c” : 12}, ‘is’ : {“a” : 15, “b” : 19, “c” : 20}, ‘best’ :{“a” : 5, “b” : 10, “c” : 2}}, temp = “a”
Output : [7, 15, 5]
Eplanation : All values of “a” key are extracted.
Method #1 : Using list comprehension + items()
This is one of the ways in which this task can be performed. In this, we use list comprehension to perform the task of extracting particular key and items() is used to get all the items().
Python3
test_dict = { 'Gfg' : { "a" : 7 , "b" : 9 , "c" : 12 },
'is' : { "a" : 15 , "b" : 19 , "c" : 20 },
'best' :{ "a" : 5 , "b" : 10 , "c" : 2 }}
print ( "The original dictionary is : " + str (test_dict))
temp = "c"
res = [val[temp] for key, val in test_dict.items() if temp in val]
print ( "The extracted values : " + str (res))
|
Output
The original dictionary is : {'Gfg': {'a': 7, 'b': 9, 'c': 12}, 'is': {'a': 15, 'b': 19, 'c': 20}, 'best': {'a': 5, 'b': 10, 'c': 2}}
The extracted values : [12, 20, 2]
Time Complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary Space: O(m), where m is the number of values that have the key we are looking for.
Method #2 : Using list comprehension + values() + keys()
The combination of above functions can be used to solve this problem. In this, we use values() and keys() to get values and keys separately rather than at once extracted using items().
Python3
test_dict = { 'Gfg' : { "a" : 7 , "b" : 9 , "c" : 12 },
'is' : { "a" : 15 , "b" : 19 , "c" : 20 },
'best' :{ "a" : 5 , "b" : 10 , "c" : 2 }}
print ( "The original dictionary is : " + str (test_dict))
temp = "c"
res = [sub[temp] for sub in test_dict.values() if temp in sub.keys()]
print ( "The extracted values : " + str (res))
|
Output
The original dictionary is : {'Gfg': {'a': 7, 'b': 9, 'c': 12}, 'is': {'a': 15, 'b': 19, 'c': 20}, 'best': {'a': 5, 'b': 10, 'c': 2}}
The extracted values : [12, 20, 2]
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(m), where m is the number of values that contain the key ‘c’.
Method 3 : using a for loop and if condition.
Here are the steps:
- Initialize an empty list to store the values of the particular key.
- Iterate over each value in the dictionary using a for loop.
- Check if the particular key exists in the current value using an if condition.
- If the key exists, append its value to the list initialized in step 1.
- Print the list containing the extracted values.
Python3
test_dict = { 'Gfg' : { 'a' : 7 , 'b' : 9 , 'c' : 12 },
'is' : { 'a' : 15 , 'b' : 19 , 'c' : 20 },
'best' : { 'a' : 5 , 'b' : 10 , 'c' : 2 }}
print ( "The original dictionary is : " + str (test_dict))
temp = "c"
res = []
for value in test_dict.values():
if temp in value:
res.append(value[temp])
print ( "The extracted values : " + str (res))
|
Output
The original dictionary is : {'Gfg': {'a': 7, 'b': 9, 'c': 12}, 'is': {'a': 15, 'b': 19, 'c': 20}, 'best': {'a': 5, 'b': 10, 'c': 2}}
The extracted values : [12, 20, 2]
Time Complexity: O(n*m), where n is the number of values in the dictionary and m is the maximum number of keys in a value.
Auxiliary Space: O(k), where k is the number of values in the list ‘res’ containing the extracted values.
Please Login to comment...