Python - Convert Nested dictionary to Mapped Tuple - GeeksforGeeks

03 Jul, 2020
Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert nested dictionaries to mapped tuple. This kind of problem can occur in web development and day-day programming. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = {‘gfg’ : {‘x’ : 5, ‘y’ : 6, ‘z’: 3}, ‘best’ : {‘x’ : 8, ‘y’ : 3, ‘z’: 5}}Output : [(‘x’, (5, 8)), (‘y’, (6, 3)), (‘z’, (3, 5))]
Input : test_dict = {‘gfg’ : {‘x’ : 5, ‘y’ : 6, ‘z’: 3}}Output : [(‘x’, (5, )), (‘y’, (6, )), (‘z’, (3, ))]
Method #1 : Using list comprehension + generator expressionThe combination of above functions can be used to solve this problem. In this, we perform the tasks of creating the tuple using list comprehension and generator expression helps in grouping, by extracting values using values().
# Python3 code to demonstrate working of # Convert Nested dictionary to Mapped Tuple# Using list comprehension + generator expression  # initializing dictionarytest_dict = {'gfg' : {'x' : 5, 'y' : 6}, 'is' : {'x' : 1, 'y' : 4},                                      'best' : {'x' : 8, 'y' : 3}}  # printing original dictionaryprint("The original dictionary is : " + str(test_dict))  # Convert Nested dictionary to Mapped Tuple# Using list comprehension + generator expressionres = [(key, tuple(sub[key] for sub in test_dict.values()))                                for key in test_dict['gfg']]  # printing result print("The grouped dictionary : " + str(res)) 
The original dictionary is : {‘gfg’: {‘x’: 5, ‘y’: 6}, ‘is’: {‘x’: 1, ‘y’: 4}, ‘best’: {‘x’: 8, ‘y’: 3}}The grouped dictionary : [(‘x’, (5, 1, 8)), (‘y’, (6, 4, 3))]
 
Method #2 : Using defaultdict() + loopThis is yet another way in which this task can be performed. In this, we perform the task of mapping using defaultdict(), of keys of nested dictionaries and recreate value list using loop. This method is useful for older version of Python.
# Python3 code to demonstrate working of # Convert Nested dictionary to Mapped Tuple# Using defaultdict() + loopfrom collections import defaultdict      # initializing dictionarytest_dict = {'gfg' : {'x' : 5, 'y' : 6}, 'is' : {'x' : 1, 'y' : 4},                                       'best' : {'x' : 8, 'y' : 3}}  # printing original dictionaryprint("The original dictionary is : " + str(test_dict))  # Convert Nested dictionary to Mapped Tuple# Using defaultdict() + loopres = defaultdict(tuple)for key, val in test_dict.items():    for ele in val:        res[ele] += (val[ele], )  # printing result print("The grouped dictionary : " + str(list(res.items())) 
The original dictionary is : {‘gfg’: {‘x’: 5, ‘y’: 6}, ‘is’: {‘x’: 1, ‘y’: 4}, ‘best’: {‘x’: 8, ‘y’: 3}}The grouped dictionary : [(‘x’, (5, 1, 8)), (‘y’, (6, 4, 3))]
Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python program to check whether a number is Prime or not
Python | Convert a list to dictionary