Python program to Convert Matrix to Dictionary Value List
Last Updated :
27 Feb, 2023
Given Matrix, the task is to write a Python program to map each column’s values as customized keys from another list.
Input : test_list = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]], map_list = [4, 5, 6]
Output : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Explanation : 4 is mapped with all the 0th index of lists, 4, 1 ,3, 10.
Input : test_list = [[4, 5, 6], [1, 3, 5], [3, 8, 1]], map_list = [4, 5, 6]
Output : {4: [4, 1, 3], 5: [5, 3, 8], 6: [6, 5, 1]}
Explanation : 4 is mapped with all the 0th index of lists, 4, 1 ,3.
Method 1 : Using dictionary comprehension + zip()
In this, mapping of columns with custom list index elements is done using zip(), dictionary comprehension is done to assign extracted keys to mapping values.
Python3
from collections import defaultdict
test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
map_list = [ 4 , 5 , 6 ]
temp = [{key : val for key,
val in zip (map_list, idx)} for idx in test_list]
res = defaultdict( list )
{res[key].append(sub[key]) for sub in temp for key in sub}
print ( "Converted Dictionary : " + str ( dict (res)))
|
Output
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 2 : Using dict() + list comprehension + zip()
In this, the task of mapping values to dictionary keys and conversion is done using dict() and zip() and dictionary comprehension. Rest functionalities are similar to the above method.
Python3
from collections import defaultdict
test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
map_list = [ 4 , 5 , 6 ]
temp = [ dict ( zip (map_list, sub)) for sub in test_list]
res = defaultdict( list )
{res[key].append(sub[key]) for sub in temp for key in sub}
print ( "Converted Dictionary : " + str ( dict (res)))
|
Output
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Method 3: Using loop
First initializes an empty dictionary res. Then, it loops over each row of the matrix test_list. For each row, it loops over each element of the row using a range function. For each element, it checks whether the corresponding key already exists in the res dictionary. If not, it creates a new key with an empty list as its value. Finally, it appends the element to the corresponding list. After the loops are completed, the resulting dictionary is printed.
Python3
test_list = [[ 4 , 5 , 6 ], [ 1 , 3 , 5 ], [ 3 , 8 , 1 ], [ 10 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
map_list = [ 4 , 5 , 6 ]
res = {}
for row in test_list:
for i in range ( len (row)):
if map_list[i] not in res:
res[map_list[i]] = []
res[map_list[i]].append(row[i])
print ( "Converted Dictionary : " + str (res))
|
Output
The original list is : [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]]
Converted Dictionary : {4: [4, 1, 3, 10], 5: [5, 3, 8, 3], 6: [6, 5, 1, 5]}
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Share your thoughts in the comments
Please Login to comment...