Python – Flatten Nested Dictionary to Matrix
Sometimes, while working with data, we can have a problem in which we need to convert a nested dictionary into Matrix, each nesting comprising the different rows in the matrix. This can have applications in many data domains. Let us discuss certain ways in which this task can be performed.
Method #1 : Using loop + zip() + map()
The combination of the above functions can be used to perform this task. In this, we use brute force to flatten the dictionary keys and then use map() and zip() to align them as rows of a matrix.
Python3
test_dict = { 'Gfg1' : { 'CS' : 1 , 'GATE' : 2 },
'Gfg2' : { 'CS' : 2 , 'GATE' : 3 },
'Gfg3' : { 'CS' : 4 , 'GATE' : 5 }}
print ( "The original dictionary is : " + str (test_dict))
temp = list (test_dict.values())
sub = set ()
for ele in temp:
for idx in ele:
sub.add(idx)
res = []
res.append(sub)
for key, val in test_dict.items():
temp2 = []
for idx in sub:
temp2.append(val.get(idx, 0 ))
res.append(temp2)
res = [[idx for idx, val in test_dict.items()]] + list ( map ( list , zip ( * res)))
print ( "The Grouped dictionary list is : " + str (res))
|
Output :
The original dictionary is : {‘Gfg3’: {‘GATE’: 5, ‘CS’: 4}, ‘Gfg1’: {‘GATE’: 2, ‘CS’: 1}, ‘Gfg2’: {‘GATE’: 3, ‘CS’: 2}}
The Grouped dictionary list is : [[‘Gfg3’, ‘Gfg1’, ‘Gfg2’], [‘GATE’, 5, 2, 3], [‘CS’, 4, 1, 2]]
Time Complexity: O(n*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 union() + list comprehension
A combination of the above methods can be used to perform this task. In this, we compute the union using union() rather than the nested loops. The result is compiled using list comprehension.
Python3
test_dict = { 'Gfg1' : { 'CS' : 1 , 'GATE' : 2 },
'Gfg2' : { 'CS' : 2 , 'GATE' : 3 },
'Gfg3' : { 'CS' : 4 , 'GATE' : 5 }}
print ( "The original dictionary is : " + str (test_dict))
temp = set ().union( * test_dict.values())
res = [ list (test_dict.keys())]
res + = [[key] + [sub.get(key, 0 ) for sub in test_dict.values()]
for key in temp]
print ( "The Grouped dictionary list is : " + str (res))
|
Output :
The original dictionary is : {‘Gfg3’: {‘GATE’: 5, ‘CS’: 4}, ‘Gfg1’: {‘GATE’: 2, ‘CS’: 1}, ‘Gfg2’: {‘GATE’: 3, ‘CS’: 2}}
The Grouped dictionary list is : [[‘Gfg3’, ‘Gfg1’, ‘Gfg2’], [‘GATE’, 5, 2, 3], [‘CS’, 4, 1, 2]]
Method #3: Flatten a nested dictionary to a matrix using pandas library
Use the pandas library to flatten a nested dictionary into a matrix. It creates a DataFrame from the dictionary, fills the missing values with 0, and then converts it to a list of lists where each sublist represents a row in the matrix. The first sublist contains the keys of the inner dictionaries, and the remaining sublists contain the corresponding values.
Python3
import pandas as pd
test_dict = { 'Gfg1' : { 'CS' : 1 , 'GATE' : 2 },
'Gfg2' : { 'CS' : 2 , 'GATE' : 3 },
'Gfg3' : { 'CS' : 4 , 'GATE' : 5 }}
print ( "The original dictionary is : " + str (test_dict))
df = pd.DataFrame(test_dict).T.fillna( 0 )
res = df.reset_index().rename(columns = { 'index' : 'Grouped' })
res = [ list (res.columns)] + res.values.tolist()
print ( "The Grouped dictionary list is : " + str (res))
|
OUTPUT:
The original dictionary is : {'Gfg1': {'CS': 1, 'GATE': 2}, 'Gfg2': {'CS': 2, 'GATE': 3}, 'Gfg3': {'CS': 4, 'GATE': 5}}
The Grouped dictionary list is : [['Grouped', 'CS', 'GATE'], ['Gfg1', 1, 2], ['Gfg2', 2, 3], ['Gfg3', 4, 5]]
Time complexity: O(nm), where n is the number of keys in the outer dictionary and m is the number of keys in the inner dictionary with the most number of keys.
Auxiliary space: O(nm), as it creates a pandas.
Method #4: Using dictionary comprehension and itertools.chain() function
The program initializes a nested dictionary, flattens it to a matrix using dictionary comprehension and itertools.chain() function, and prints the original dictionary and the resulting matrix.
- Import the itertools module.
- Define the nested dictionary test_dict.
- Use dictionary comprehension and itertools.chain() function to flatten the nested dictionary to a matrix.
- Create a list res that consists of the headers for the table, followed by the data rows.
- Print the resulting matrix.
Python3
import itertools
test_dict = { 'Gfg1' : { 'CS' : 1 , 'GATE' : 2 },
'Gfg2' : { 'CS' : 2 , 'GATE' : 3 },
'Gfg3' : { 'CS' : 4 , 'GATE' : 5 }}
print ( "The original dictionary is : " + str (test_dict))
res = [[ 'Grouped' , 'CS' , 'GATE' ]] + [[k] + list (v.values()) for k, v in test_dict.items()]
print ( "The Grouped dictionary list is : " + str (res))
|
Output
The original dictionary is : {'Gfg1': {'CS': 1, 'GATE': 2}, 'Gfg2': {'CS': 2, 'GATE': 3}, 'Gfg3': {'CS': 4, 'GATE': 5}}
The Grouped dictionary list is : [['Grouped', 'CS', 'GATE'], ['Gfg1', 1, 2], ['Gfg2', 2, 3], ['Gfg3', 4, 5]]
Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(n), where n is the number of elements in the dictionary, to store the resulting matrix.
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...