Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Extract values of Particular Key in Nested Values

Python – Extract values of Particular Key in Nested Values

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




# Python3 code to demonstrate working of
# Extract values of Particular Key in Nested Values
# Using list comprehension
 
# initializing dictionary
test_dict = {'Gfg' : {"a" : 7, "b" : 9, "c" : 12},
             'is' : {"a" : 15, "b" : 19, "c" : 20},
             'best' :{"a" : 5, "b" : 10, "c" : 2}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing key
temp = "c"
 
# using item() to extract key value pair as whole
res = [val[temp] for key, val in test_dict.items() if temp in val]
 
# printing result
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




# Python3 code to demonstrate working of
# Extract values of Particular Key in Nested Values
# Using list comprehension + values() + keys()
 
# initializing dictionary
test_dict = {'Gfg' : {"a" : 7, "b" : 9, "c" : 12},
             'is' : {"a" : 15, "b" : 19, "c" : 20},
             'best' :{"a" : 5, "b" : 10, "c" : 2}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing key
temp = "c"
 
# using keys() and values() to extract values
res = [sub[temp] for sub in test_dict.values() if temp in sub.keys()]
 
# printing result
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:

  1. Initialize an empty list to store the values of the particular key.
  2. Iterate over each value in the dictionary using a for loop.
  3. Check if the particular key exists in the current value using an if condition.
  4. If the key exists, append its value to the list initialized in step 1.
  5. Print the list containing the extracted values.

Python3




# initializing dictionary
test_dict = {'Gfg': {'a': 7, 'b': 9, 'c': 12},
             'is': {'a': 15, 'b': 19, 'c': 20},
             'best': {'a': 5, 'b': 10, 'c': 2}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing key
temp = "c"
 
# initializing empty list
res = []
 
# iterating over each value in dictionary
for value in test_dict.values():
     
    # checking if key exists in current value
    if temp in value:
         
        # appending value of key to the result list
        res.append(value[temp])
 
# printing result
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. 

RELATED ARTICLES

Most Popular

Recent Comments