Skip to content
Related Articles

Related Articles

Python – Extract selective keys’ values Including Nested Keys

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jan, 2022

Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract selective keys’ values. This problem has been solved earlier, but sometimes, we can have multiple nestings and certain keys may be present in inner records. This problem caters all the nestings for extraction of keys’ values. Let’s discuss certain way in which this task can be solved.

test_dict = {‘gfg’: {‘geeks’: {‘best’ : 3}}} 
key_list = [‘best’, ‘geeks’] 
Output : {‘geeks’: {‘best’: 3}, ‘best’: 3}
test_dict = {‘gfg’: {‘geek’: {‘good’ : 3}}} 
key_list = [‘best’, ‘geeks’] 
Output : {} 

Method : Using recursion + loop + yield 
The combination of above functionalities can be used to solve this problem. In this, we perform the task of checking for key using conditional statements and check for nestings using recursion. The yield operator is used to dynamically return the key for assignment as it occurs.


# Python3 code to demonstrate working of
# Extract selective keys' values [ Including Nested Keys ]
# Using recursion + loop + yield
def get_vals(test_dict, key_list):
   for i, j in test_dict.items():
     if i in key_list:
        yield (i, j)
     yield from [] if not isinstance(j, dict) else get_vals(j, key_list)
# initializing dictionary
test_dict = {'gfg': {'is': {'best' : 3}}, 'for': {'all' : 4}, 'geeks': 5}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing keys list
key_list = ['best', 'geeks']
# Extract selective keys' values [ Including Nested Keys ]
# Using recursion + loop + yield
res = dict(get_vals(test_dict, key_list))
# printing result
print("The extracted values : " + str(res))

Output : 

The original dictionary is : {'gfg': {'is': {'best': 3}}, 'for': {'all': 4}, 'geeks': 5}
The extracted values : {'best': 3, 'geeks': 5}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!