Sometimes, while working with Python Dictionaries, we can have nesting of dictionaries, with each key being single values dictionary. In this we need to remove the top level of dictionary. This can have application in data preprocessing. Lets discuss certain ways in which this task can be performed.
Method #1 : Using values() + dictionary comprehension The combination of above functions can be used to solve this problem. In this, we perform the task of dictionary reconstruction using dictionary comprehension and nested lists are extracted using values().
Python3
# Python3 code to demonstrate working of # Remove Top level from Dictionary # Using dictionary comprehension + values() # initializing dictionary test_dict = { 'gfg' : { 'data1' : [ 4 , 5 , 6 , 7 ]}, 'is' : { 'data2' : [ 1 , 3 , 8 ]}, 'best' : { 'data3' : [ 9 , 10 , 13 ]}} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Remove Top level from Dictionary # Using dictionary comprehension + values() res = dict (ele for sub in test_dict.values() for ele in sub.items()) # printing result print ("The top level removed dictionary is : " + str (res)) |
The original dictionary is : {‘is’: {‘data2’: [1, 3, 8]}, ‘gfg’: {‘data1’: [4, 5, 6, 7]}, ‘best’: {‘data3’: [9, 10, 13]}} The top level removed dictionary is : {‘data1’: [4, 5, 6, 7], ‘data2’: [1, 3, 8], ‘data3’: [9, 10, 13]}
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using ChainMap() + dict() The combination of above functionalities can also be used to solve this problem. In this, we employ ChainMap() to perform mapping of nested values.
Python3
# Python3 code to demonstrate working of # Remove Top level from Dictionary # Using ChainMap() + dict() from collections import ChainMap # initializing dictionary test_dict = { 'gfg' : { 'data1' : [ 4 , 5 , 6 , 7 ]}, 'is' : { 'data2' : [ 1 , 3 , 8 ]}, 'best' : { 'data3' : [ 9 , 10 , 13 ]}} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Remove Top level from Dictionary # Using ChainMap() + dict() res = dict (ChainMap( * test_dict.values())) # printing result print ("The top level removed dictionary is : " + str (res)) |
The original dictionary is : {‘is’: {‘data2’: [1, 3, 8]}, ‘gfg’: {‘data1’: [4, 5, 6, 7]}, ‘best’: {‘data3’: [9, 10, 13]}} The top level removed dictionary is : {‘data1’: [4, 5, 6, 7], ‘data2’: [1, 3, 8], ‘data3’: [9, 10, 13]}
Using loops and dictionary:
Approach:
In this approach, we will use nested loops to iterate through the original dictionary and create a new dictionary with the top level removed.
Python3
original_dict = { 'is' : { 'data2' : [ 1 , 3 , 8 ]}, 'gfg' : { 'data1' : [ 4 , 5 , 6 , 7 ]}, 'best' : { 'data3' : [ 9 , 10 , 13 ]}} new_dict = {} for key, value in original_dict.items(): for sub_key, sub_value in value.items(): new_dict[sub_key] = sub_value print (new_dict) |
{'data2': [1, 3, 8], 'data1': [4, 5, 6, 7], 'data3': [9, 10, 13]}
Time complexity: O(n^2), where n is the size of the list
Space complexity: O(n)