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.
Method #1 : Using loop
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.
Python3
# Python3 code to demonstrate working of # Column Mapped Tuples to dictionary items # Using loop # initializing list test_list = [[( 5 , 6 ), ( 7 , 4 ), ( 1 , 2 )], [( 7 , 3 ), ( 10 , 14 ), ( 11 , 22 )] ] # printing original list print ( "The original list : " + str (test_list)) res = dict () # loop for tuple lists for idx in range ( len (test_list) - 1 ): for idx2 in range ( len (test_list[idx])): # column wise dictionary pairing res[test_list[idx][idx2][ 0 ]] = test_list[idx + 1 ][idx2][ 0 ] res[test_list[idx][idx2][ 1 ]] = test_list[idx + 1 ][idx2][ 1 ] # printing result print ( "The constructed dictionary : " + str (res)) |
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 elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method #2 : Using dictionary comprehension + zip()
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.
Python3
# Python3 code to demonstrate working of # Column Mapped Tuples to dictionary items # Using dictionary comprehension + zip() # initializing list test_list = [[( 5 , 6 ), ( 7 , 4 ), ( 1 , 2 )], [( 7 , 3 ), ( 10 , 14 ), ( 11 , 22 )] ] # printing original list print ( "The original list : " + str (test_list)) # nested dictionary comprehension to form pairing # paired using zip() res = {key[idx] : val[idx] for key, val in zip ( * tuple (test_list)) for idx in range ( len (key))} # printing result print ( "The constructed dictionary : " + str (res)) |
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”.
Method 3: Using itertools and reduce
- Import the itertools and functools libraries.
- Use the itertools.zip_longest() function to zip together the tuples in the test_list.
- Use the functools.reduce() function to iterate over the zipped tuples and construct the dictionary.
- For each tuple, use a dictionary comprehension to construct a dictionary where the first element of the tuple is the key and the second element is the value.
- Merge the dictionaries using the dict.update() method.
Python3
import itertools import functools test_list = [[( 5 , 6 ), ( 7 , 4 ), ( 1 , 2 )], [( 7 , 3 ), ( 10 , 14 ), ( 11 , 22 )] ] zipped = itertools.zip_longest( * test_list) res = functools. reduce ( lambda x, y: dict ( list (x.items()) + list (y.items())), ({k:v for k, v in zip (i, j)} for i, j in zipped), {}) print ( "The constructed dictionary : " + str (res)) |
The 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.