Python – Swapping Hierarchy in Nested Dictionaries

16 Jun, 2021
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform hierarchy swapping of nested dictionaries. This problem can have application in domains which require dictionary restructuring. Let’s discuss certain ways in which this task can be performed. 
Input : test_dict = {‘Gfg’: { ‘a’ : [1, 3, 7, 8], ‘b’ : [4, 9], ‘c’ : [0, 7]}} Output : {‘c’: {‘Gfg’: [0, 7]}, ‘b’: {‘Gfg’: [4, 9]}, ‘a’: {‘Gfg’: [1, 3, 7, 8]}}Input : test_dict = {‘Gfg’: {‘best’ : [1, 3, 4]}} Output : {‘best’: {‘Gfg’: [1, 3, 4]}}  
Method #1 : Using loop + items() The combination of above functions can be used to solve this problem. In this, we iterate the dictionary and restructure using brute force. The items() is used to extract all the key-value pairs of dictionary. 
Python3
# Python3 code to demonstrate working of# Swapping Hierarchy in Nested Dictionaries# Using loop + items() # initializing dictionarytest_dict = {'Gfg': { 'a' : [1, 3], 'b' : [3, 6], 'c' : [6, 7, 8]},             'Best': { 'a' : [7, 9], 'b' : [5, 3, 2], 'd' : [0, 1, 0]}} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # Swapping Hierarchy in Nested Dictionaries# Using loop + items()res = dict()for key, val in test_dict.items():    for key_in, val_in in val.items():        if key_in not in res:            temp = dict()        else:            temp = res[key_in]        temp[key] = val_in        res[key_in] = temp # printing resultprint("The rearranged dictionary : " + str(res))
The original dictionary : {‘Gfg’: {‘a’: [1, 3], ‘c’: [6, 7, 8], ‘b’: [3, 6]}, ‘Best’: {‘d’: [0, 1, 0], ‘a’: [7, 9], ‘b’: [5, 3, 2]}} The rearranged dictionary : {‘d’: {‘Best’: [0, 1, 0]}, ‘a’: {‘Gfg’: [1, 3], ‘Best’: [7, 9]}, ‘c’: {‘Gfg’: [6, 7, 8]}, ‘b’: {‘Gfg’: [3, 6], ‘Best’: [5, 3, 2]}} 
 
 Method #2 : Using defaultdict() + loop This is yet another brute force way to solve this problem. In this, we reduce a key test step by using defaultdict() rather than conventional dictionary. 
Python3
# Python3 code to demonstrate working of# Swapping Hierarchy in Nested Dictionaries# Using  defaultdict() + loopfrom collections import defaultdict # initializing dictionarytest_dict = {'Gfg': { 'a' : [1, 3], 'b' : [3, 6], 'c' : [6, 7, 8]},             'Best': { 'a' : [7, 9], 'b' : [5, 3, 2], 'd' : [0, 1, 0]}} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # Swapping Hierarchy in Nested Dictionaries# Using  defaultdict() + loopres = defaultdict(dict)for key, val in test_dict.items():    for key_in, val_in in val.items():        res[key_in][key] = val_in # printing resultprint("The rearranged dictionary : " + str(dict(res)))
The original dictionary : {‘Gfg’: {‘a’: [1, 3], ‘c’: [6, 7, 8], ‘b’: [3, 6]}, ‘Best’: {‘d’: [0, 1, 0], ‘a’: [7, 9], ‘b’: [5, 3, 2]}} The rearranged dictionary : {‘d’: {‘Best’: [0, 1, 0]}, ‘a’: {‘Gfg’: [1, 3], ‘Best’: [7, 9]}, ‘c’: {‘Gfg’: [6, 7, 8]}, ‘b’: {‘Gfg’: [3, 6], ‘Best’: [5, 3, 2]}} 
 
clintra
Python dictionary-programs
Python-nested-dictionary
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Convert string dictionary to dictionary