Given Tuple Matrix of length 2, map each column's element value with next column and construct dictionary keys.
Input : test_list = [[(1, 4), (6, 3), (4, 7)], [(7, 3), (10, 14), (11, 22)]] Output : {1: 7, 4: 3, 6: 10, 3: 14, 4: 11, 7: 22} Explanation : 1 -> 7, 4 -> 3.., as in same column and indices. Input : test_list = [[(1, 4), (6, 3)], [(7, 3), (10, 14)]] Output : {1: 7, 4: 3, 6: 10, 3: 14} Explanation : Self explanatory column wise pairing.
This is one of the ways in which this task can be performed. In this, we iterate for all the elements with their next column elements and construct dictionary key-value pair.
Output
The original list : [[(5, 6), (7, 4), (1, 2)], [(7, 3), (10, 14), (11, 22)]] The constructed dictionary : {5: 7, 6: 3, 7: 10, 4: 14, 1: 11, 2: 22}
The combination of above functions provides one-liner to solve this problem. In this, we perform the task of zipping all the columns using zip() and dictionary comprehension is used to key-value pairs.
Output
The original list : [[(5, 6), (7, 4), (1, 2)], [(7, 3), (10, 14), (11, 22)]] The constructed dictionary : {5: 7, 6: 3, 7: 10, 4: 14, 1: 11, 2: 22}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
OutputThe constructed dictionary : {5: 7, 6: 3, 7: 10, 4: 14, 1: 11, 2: 22}
Time complexity: O(nm), where n is the length of the test_list and m is the maximum length of the tuples in the list.
Auxiliary space: O(nm), where n is the length of the test_list and m is the maximum length of the tuples in the list.