Open In App

Python – Remove Top level from Dictionary

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output : 

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))


Output : 

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)


Output

{'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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads