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 expression The 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
# Python3 code to demonstrate working of # Convert Nested dictionary to Mapped Tuple # Using list comprehension + generator expression # initializing dictionary test_dict = { 'gfg' : { 'x' : 5 , 'y' : 6 }, 'is' : { 'x' : 1 , 'y' : 4 }, 'best' : { 'x' : 8 , 'y' : 3 }} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Convert Nested dictionary to Mapped Tuple # Using list comprehension + generator expression res = [(key, tuple (sub[key] for sub in test_dict.values())) for key in test_dict[ 'gfg' ]] # printing result print ("The grouped dictionary : " + str (res)) |
Method #2 : Using defaultdict() + loop This 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
# Python3 code to demonstrate working of # Convert Nested dictionary to Mapped Tuple # Using defaultdict() + loop from collections import defaultdict # initializing dictionary test_dict = { 'gfg' : { 'x' : 5 , 'y' : 6 }, 'is' : { 'x' : 1 , 'y' : 4 }, 'best' : { 'x' : 8 , 'y' : 3 }} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Convert Nested dictionary to Mapped Tuple # Using defaultdict() + loop res = 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 Time and Space Complexity is the same for both methods:
Time Complexity: O(n2)
Space Complexity: O(n)
Method#3: Using zip() function and dictionary operations
This method uses the zip() function and dictionary operations to convert a nested dictionary to a mapped tuple.
Algorithm:
- Initialize an empty list res to store the mapped tuples.
- Loop through the keys in the dictionary of interest (here, the dictionary with key ‘gfg’ is chosen as the target dictionary).
- For each key, create a tuple of the corresponding values for that key from all sub-dictionaries, using dictionary operations.
- Append the tuple to the res list.
- Return the res list.
Python
# initializing dictionary test_dict = { 'gfg' : { 'x' : 5 , 'y' : 6 }, 'is' : { 'x' : 1 , 'y' : 4 }, 'best' : { 'x' : 8 , 'y' : 3 }} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Nested dictionary to Mapped Tuple # Using zip() function and dictionary operations res = [(key, tuple (test_dict[k][key] for k in test_dict)) for key in test_dict[ 'gfg' ]] # printing result print ( "The grouped dictionary : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original dictionary is : {'is': {'y': 4, 'x': 1}, 'gfg': {'y': 6, 'x': 5}, 'best': {'y': 3, 'x': 8}} The grouped dictionary : [('y', (4, 6, 3)), ('x', (1, 5, 8))]
Time complexity: O(NM), where N is the number of keys in the dictionary of interest, and M is the number of sub-dictionaries.
Auxiliary space: O(NM), to store the res list.