Open In App

Python Program to Convert Tuple Matrix to Tuple List

Given a Tuple Matrix, flatten to tuple list with each tuple representing each column.

Example:

Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] 
Output : [(4, 7, 10, 18), (5, 8, 13, 17)] 
Explanation : All column number elements contained together. 

Input : test_list = [[(4, 5)], [(10, 13)]] 
Output : [(4, 10), (5, 13)] 
Explanation : All column number elements contained together.

Convert Tuple Matrix to Tuple List Using list comprehension + zip()

In this, we perform task of flattening using list comprehension and zip() is used to perform column pairing to render as tuple pairs. 

Steps:

  1. The program initializes a list called test_list with three elements, each of which is a list of tuples.
  2. It prints the original list using the print() function and string concatenation.
  3. The program uses a list comprehension to flatten the test_list. The ele variable represents each individual element in the sublists, while the sub variable represents each sublist in the test_list. The flattened list is stored in the variable temp.
  4. The program uses the zip() function to join the tuples in temp into column pairs, which are then stored in the variable res. Note that the * operator is used to unpack the elements in temp as arguments to zip().
  5. The program prints the result using the print() function and string concatenation.




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using list comprehension + zip()
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# flattening
temp = [ele for sub in test_list for ele in sub]
 
# joining to form column pairs
res = list(zip(*temp))
 
# printing result
print("The converted tuple list : " + str(res))

Output
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

Convert Tuple Matrix to Tuple List Using chain.from_iterable() + zip()

In this, task of flattening is performed using chain.from_iterable() and zip() is used to perform the task of column pairing.




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using chain.from_iterable() + zip()
from itertools import chain
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# flattening using from_iterable
res = list(zip(*chain.from_iterable(test_list)))
 
# printing result
print("The converted tuple list : " + str(res))

Output
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

Time complexity: O(nm)
Auxiliary space: O(nm),

Convert Tuple Matrix to Tuple List Using extend(),list() and tuple() methods




# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
x=[]
for i in test_list:
    for j in i:
        j=list(j)
        x.extend(j)
re1=[]
re2=[]
for i in range(0,len(x)):
    if(i%2==0):
        re1.append(x[i])
    else:
        re2.append(x[i])
res=[]
res.append(tuple(re1))
res.append(tuple(re2))
 
# printing result
print("The converted tuple list : " + str(res))

Output
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

The time complexity is O(n^2), where n is the total number of elements in the input matrix. 
The auxiliary space complexity of this code is O(n), as it uses a list x to store all the elements in the matrix and two lists re1 and re2 to store the elements after converting them to a tuple list.

Convert Tuple Matrix to Tuple List Using lambda function 

This code converts a matrix represented as a tuple of tuples, matrix, into a tuple of lists, list_tuple. It uses map() and a lambda function to apply list() to each tuple in matrix, converting them into lists. The resulting tuple of lists is then printed.




matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
 
list_tuple = tuple(map(lambda x: list(x), matrix))
 
print(list_tuple)  # Output: ([1, 2, 3], [4, 5, 6], [7, 8, 9])

Output
([1, 2, 3], [4, 5, 6], [7, 8, 9])

Time complexity: O(mn), where m is the number of rows in the matrix and n is the number of columns in the matrix. This is because the map function is applied to each inner tuple in the matrix, and converting each inner tuple to a list takes O(n) time. Since there are m inner tuples, the total time complexity is O(mn).
Auxiliary space: O(mn), which represents the memory used to store the final tuple of lists. This is because each inner tuple is converted to a list, and all the lists are stored in the final tuple. The amount of memory used is proportional to the size of the matrix.

Convert Tuple Matrix to Tuple List Using NumPy module

Step-by-step algorithm:

  1. Initialize the list test_list with the given input.
  2. Create a numpy array by flattening test_list into a 1-dimensional array of integers using a list comprehension and the numpy function array. 
  3. The data type is specified as np.int64.
  4. Transpose the array using the numpy function T.
  5. Convert the transposed array into a list of tuples using the tuple and map functions.
  6. Assign the resulting list of tuples to the variable result.
  7. Print the final result.




import numpy as np
 
# Initialize the list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# Flatten the list and convert it to a numpy array
array = np.array([col for sublist in test_list for col in sublist], np.int64)
 
# Transpose the array and convert it to a tuple
result = tuple(map(tuple, array.T))
 
# Print the result
print("The converted tuple list:", result)

output:

The converted tuple list : [([4, 10, 0], [7, 18, 10]), ([5, 13, 4], [8, 17, 1])

Time complexity: O(n), where n is the total number of elements in the input list test_list. This is because the algorithm involves iterating over each element in the list once to flatten it into a 1-dimensional numpy array, then transposing the array, and finally converting the transposed array into a list of tuples. The time complexity of these operations is linear with respect to the number of elements in the input list.
Auxiliary space: O(n), where n is the total number of elements in the input list test_list. This is because the algorithm creates a numpy array of the same size as the flattened input list, which requires additional memory. The space used by the result variable is also proportional to the number of elements in the input list. Overall, the space complexity of the algorithm is linear with respect to the number of elements in the input list.

Convert Tuple Matrix to Tuple List Using Recursive method.

Step-by-step algorithm:

  1. Define a function flatten(lst) that takes a list as input.
  2. If the input list lst is a list, then recursively flatten each sublist within lst using a list comprehension that iterates over each sublist element and applies the flatten() function to it. If the element is not a list, then simply return the element as a list.
  3. Once all the sublists have been flattened, concatenate them into a single list using another list comprehension.
  4. Convert the flattened list of tuples into a list of columns by using the zip() function with the * operator to unpack the tuples into separate arguments for zip().
  5. Return the resulting list of columns.




# define a recursive function to flatten the list
def flatten(lst):
    if isinstance(lst, list):
        return [ele for sub in lst for ele in flatten(sub)]
    else:
        return [lst]
 
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# flattening and converting to tuple list
temp = flatten(test_list)
res = list(zip(*temp))
 
# printing result
print("The converted tuple list : " + str(res))

Output
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]

Time complexity: O(n), where n is the total number of elements in the input list. 
Auxiliary space: O(n), since the flatten () function creates a new list containing all the elements of the input list.


Article Tags :