![]() |
VOOZH | about |
Given a Matrix and list of keys, map each column's values with a custom list key.
Input : test_list1 = [[4, 6], [8, 6]], test_list2 = ["Gfg", "Best"]
Output : {'Gfg': [4, 8], 'Best': [6, 6]}
Explanation : Column wise, Key values assignment.Input : test_list1 = [[4], [6]], test_list2 = ["Gfg"]
Output : {'Gfg': [4, 6]}
Explanation : Column wise, Key values assignment, just single element list. Method #1 : Using list comprehension + dictionary comprehension
The combination of the above functionality can be used to solve this problem. In this, we perform the task of extracting column values using list comprehension, and then dictionary comprehension is used to form a dictionary with list keys.
The original list 1 : [[4, 6, 8], [8, 4, 2], [8, 6, 3]]
The original list 2 : ['Gfg', 'is', 'Best']
The paired dictionary : {'Gfg': [4, 8, 8], 'is': [6, 4, 6], 'Best': [8, 2, 3]}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
Method #2 : Using zip() + dict()
This is yet another way in which this task can be performed. In this, we join key-value pair using zip() and dict() is used to convert result in the dictionary. The difference is that it generates tuples rather than lists as mapped values.
The original list 1 : [[4, 6, 8], [8, 4, 2], [8, 6, 3]]
The original list 2 : ['Gfg', 'is', 'Best']
The paired dictionary : {'Gfg': (4, 8, 8), 'is': (6, 4, 6), 'Best': (8, 2, 3)}Method #3: Using nested loops
The original list 1 : [[4, 6, 8], [8, 4, 2], [8, 6, 3]]
The original list 2 : ['Gfg', 'is', 'Best']
The paired dictionary : {'Gfg': [4, 8, 8], 'is': [6, 4, 6], 'Best': [8, 2, 3]}Time Complexity: O(M*N) , M is the length of list of lists, N - length of the list
Auxiliary Space: O(M) , M - length of dictionary
Method #4: Using NumPy
This approach uses NumPy's transpose() function to extract the columns from the matrix and then creates a dictionary using the elements of the given list as keys and the extracted columns as values. This method is efficient and requires less code.
OUTPUT :
The original list 1 : [[4, 6, 8], [8, 4, 2], [8, 6, 3]]
The original list 2 : ['Gfg', 'is', 'Best']
The paired dictionary : {'Gfg': array([4, 8, 8]), 'is': array([6, 4, 6]), 'Best': array([8, 2, 3])}The time and auxiliary space complexity of this method is O(n^2) where n is the length of the matrix.
Method #5: Using a nested list comprehension and the zip() function
Steps:
The original list 1 : [[4, 6, 8], [8, 4, 2], [8, 6, 3]]
The original list 2 : ['Gfg', 'is', 'Best']
The paired dictionary : {'Gfg': [4, 8, 8], 'is': [6, 4, 6], 'Best': [8, 2, 3]}Time Complexity: O(N*M), where N is the number of lists in the test_list1 and M is the length of each list in the test_list1. This is because we must iterate through each element in each list in test_list1 in order to transpose it.
Auxiliary Space: O(NM), where N is the number of lists in test_list1 and M is the length of each list in test_list1. This is because we create a new list with the transposed matrix, which will have NM elements. Additionally, we create a dictionary with N keys and M values each, resulting in N*M total elements.