Open In App
Related Articles

Python – Mapping Matrix with Dictionary

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a Matrix, map its values with dictionary values.

Input : test_list = [[4, 2, 1], [1, 2, 3]], sub_dict = {1 : “gfg”, 2: “best”, 3 : “CS”, 4 : “Geeks”} Output : [[‘Geeks’, ‘best’, ‘gfg’], [‘gfg’, ‘best’, ‘CS’]] Explanation : Matrix elements are substituted using dictionary. Input : test_list = [[4, 2, 1]], sub_dict = {1 : “gfg”, 2: “best”, 4 : “Geeks”} Output : [[‘Geeks’, ‘best’, ‘gfg’]] Explanation : Matrix elements are substituted using dictionary.

Method #1 : Using loop

This is brute way in which this task can be performed. In this, we iterate for all the elements of Matrix and map from dictionary.

Python3

# Python3 code to demonstrate working of
# Mapping Matrix with Dictionary
# Using loop
 
# initializing list
test_list = [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing dictionary
sub_dict = {1 : "gfg", 2: "best", 3 : "CS", 4 : "Geeks"}
 
# Using loop to perform required mapping
res = []
for sub in test_list:
    temp = []
    for ele in sub:
         
        # mapping values from dictionary
        temp.append(sub_dict[ele])
    res.append(temp)
 
# printing result
print("Converted Mapped Matrix : " + str(res))

                    

Output
The original list : [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
Converted Mapped Matrix : [['Geeks', 'best', 'gfg'], ['gfg', 'best', 'CS'], ['Geeks', 'CS', 'gfg']]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The sorted and itemgetter function is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

Method #2 : Using list comprehension

This is yet another way in which this task can be performed. This is just shorthand to above method. 

Python3

# Python3 code to demonstrate working of
# Mapping Matrix with Dictionary
# Using list comprehension
 
# initializing list
test_list = [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing dictionary
sub_dict = {1 : "gfg", 2: "best", 3 : "CS", 4 : "Geeks"}
 
# Using list comprehension to perform required mapping
# in one line
res = [[sub_dict[val] for val in sub] for sub in test_list]
 
# printing result
print("Converted Mapped Matrix : " + str(res))

                    

Output
The original list : [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
Converted Mapped Matrix : [['Geeks', 'best', 'gfg'], ['gfg', 'best', 'CS'], ['Geeks', 'CS', 'gfg']]

Method 3 :  using the map() function:

Step-by-step approach:

Initialize the original list test_list with the matrix values.
Print the original list.
Initialize the dictionary sub_dict with the mapping values.
Use the map() function to apply the sub_dict.get method on each element of the sublists of the test_list.
Convert the output of map() to a list using the list() method.
Apply the map() function again to convert each sublist of the test_list to a list of mapped values.
Convert the final output of map() to a list using the list() method.
Print the final result.

Python3

# Python3 code to demonstrate working of
# Mapping Matrix with Dictionary
# Using map() function
 
# initializing list
test_list = [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing dictionary
sub_dict = {1 : "gfg", 2: "best", 3 : "CS", 4 : "Geeks"}
 
# Using map() function to perform required mapping
res = list(map(lambda x: list(map(sub_dict.get, x)), test_list))
 
# printing result
print("Converted Mapped Matrix : " + str(res))

                    

Output
The original list : [[4, 2, 1], [1, 2, 3], [4, 3, 1]]
Converted Mapped Matrix : [['Geeks', 'best', 'gfg'], ['gfg', 'best', 'CS'], ['Geeks', 'CS', 'gfg']]

Time complexity: The time complexity of the map function is O(n), and the time complexity of the lambda function inside it is also O(n). 

Auxiliary space: This approach uses extra memory to store the mapped matrix. The space complexity is O(n^2) as well.



Last Updated : 15 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads