Given a dictionary, perform flattening and removal of certain dictionary keys.
Input : test_dict = {‘a’: 14, ‘b’: 16, ‘c’: {‘d’: {‘e’: 7}}}, rem_keys = [“c”, “a”, “d”]
Output : {‘b’: 16, ‘e’: 7}
Explanation : All “c”, “a” and “d” has been removed.Input : test_dict = {‘a’: 14, ‘b’: 16, ‘c’: {‘d’: {‘e’: 7}}}, rem_keys = [“c”, “d”, “e”]
Output : {‘a’: 14, ‘b’: 16}
Explanation : All “c”, “e” and “d” has been removed.
Method : Using recursion + isinstance() + loop
The combination of above functions can be used to solve this problem. In this, we check for dictionary as instance for nested dictionary using isinstance() and recur each time for inner dictionary. The loop is used to iterate for keys.
Python3
# Python3 code to demonstrate working of # Flatten and remove keys from Dictonary # Using loop + recursion + isinstance() # function to compute removal and flattening def hlper_fnc(test_dict, rem_keys): if not isinstance(test_dict, dict): return test_dict res = {} for key, val in test_dict.items(): rem = hlper_fnc(val, rem_keys) # performing removal if key not in rem_keys: res[key] = rem else: if isinstance(rem, dict): res.update(rem) return res # initializing dictionary test_dict = {'a': 14, 'b': 16, 'c': {'d': {'e': 7}}} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing removal keys rem_keys = ["c", "d"] # calling helper function for task res = hlper_fnc(test_dict, rem_keys) # printing result print("The removed and flattened dictionary : " + str(res)) |
The original dictionary is : {‘a’: 14, ‘b’: 16, ‘c’: {‘d’: {‘e’: 7}}}
The removed and flattened dictionary : {‘a’: 14, ‘b’: 16, ‘e’: 7}
Recommended Posts:
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Flatten Nested Keys
- Python | Remove spaces from dictionary keys
- Python | Remove Keys from dictionary starting with K
- Python | Remove multiple keys from dictionary
- Python - Remove Disjoint Tuple keys from Dictionary
- Python - Flatten Nested Dictionary to Matrix
- Python - Flatten Dictionary with List
- Python - Extract selective keys' values Including Nested Keys
- Python | Split dictionary keys and values into separate lists
- Python program to Swap Keys and Values in Dictionary
- Python | Test if dictionary contains unique keys and values
- Python - Sort Dictionary by Values and Keys
- Python dictionary with keys having multiple inputs
- Python | Add new keys to a dictionary
- Python | Check if given multiple keys exist in a dictionary
- Python | Get dictionary keys as a list
- Python | Initialize a dictionary with only keys from a list
- Python | Ways to change keys in dictionary
- Python | Find keys with duplicate values in dictionary
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.

