Convert Matrix to Custom Tuple Matrix in Python


In Python, a tuple is one of the four built−in data types that is used to collect the data and this data is represented by round parenthesis i.e.(). Tuple is generally known for multiple collections of items into a single item. In Python, we have some built−in functions like map(), tuple(), and, lambda will be used to Convert Matrix to Custom Tuple Matrix. The custom tuple matrix is defined by a subclass of a tuple that has elements and it is separated by a comma to create the sublist(list represents matrix).

Let’s take an example of this:

Original Matrix:

[ [11, 21, 31],

[41, 51, 61],

[71, 81, 91] ]

After converting into a custom tuple matrix:(final result)

[[(11,), (21,), (31)], [(41), (51), (61)], [(71), (81), (91)]]

Syntax

The following syntax is used in the examples-

tuple()

This is a built−in function in Python that can be used to create a tuple i.e. ().

map()

The map function allows the iteration of the elements based on list, tuple, etc. to return the specific result.

lambda

The lambda function is used to represent the little function inline. The use of lambda with map() function is a common practice to iterate through list, tuple, etc.

Example 1: Using Nested List Comprehension

In the following example, the program uses the nested list which is defined by a list within the list. Using the for loop it will iterate through the list of matrix and convert the matrix into custom tuple matrix.

def matrix_to_custom_tuple(matrix):
    c_tuple_matrix = [[(value,) for value in row] for row in matrix]
    return c_tuple_matrix
# Taking sublist as an input
matrix = [[11, 92, 34], [14, 15, 16], [74, 89, 99]]
result = matrix_to_custom_tuple(matrix)
print(result)

Output

[[(11,), (92,), (34,)], [(14,), (15,), (16,)], [(74,), (89,), (99,)]]

Example 2: Using Nested for loop

In the following example, the program uses the nested for loop which means loop inside another loop and it iterates each element of the list. Using append it accepts the parameter to set the tuple and this custom tuple matrix will be created.

def matrix_to_custom_tuple(matrix):
    cus_tuple_matrix = []
    for row in matrix:
        cus_tuple_row = []
        for value in row:
            cus_tuple_row.append((value,))
        cus_tuple_matrix.append(cus_tuple_row)
    return cus_tuple_matrix
# Taking of input matrix
inp_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
answer = matrix_to_custom_tuple(inp_matrix)
print("Matrix into Custom Tuple Matrix:\n",answer)

Output

Matrix into Custom Tuple Matrix:
 [[(1,), (2,), (3,)], [(4,), (5,), (6,)], [(7,), (8,), (9,)]]

Example 3: Using map() and lambda function

In the following example, the program uses some built−in functions in Python such as list(), map(), and, lambda to convert the matrix into a custom tuple matrix.

def matrix_to_custom_tuple(matrix):
    custom_tuple_matrix = list(map(lambda row: list(map(lambda value: (value,), row)), matrix))
    return custom_tuple_matrix
matrix = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
result = matrix_to_custom_tuple(matrix)
print(result)

Output

[[('A',), ('B',), ('C',)], [('D',), ('E',), ('F',)], [('G',), ('H',), ('I',)]]

Example 4: Using list comprehension with tuple()

In the following example, the program uses list comprehension by defining various built−in functions such as list(), map(), lambda, and tuple(), and using for loop it will iterate through the list of matrix.

def matrix_to_custom_tuple(matrix):
    custom_tuple_matrix = [list(map(lambda value: tuple([value]), row)) for row in matrix]
    return custom_tuple_matrix
# Taking input to store the matrix using sublist of list
matx = [[1, 2, 3], [4, 5, 6, 7, 8], [7, 8, 9],[10, 11, 12, 13]]
result = matrix_to_custom_tuple(matx)
print("Convert the Matrix into Custom Tuple Matrix:\n",result)

Output

Convert the Matrix into Custom Tuple Matrix:
 [[(1,), (2,), (3,)], [(4,), (5,), (6,), (7,), (8,)], [(7,), (8,), (9,)], [(10,), (11,), (12,), (13,)]]

Conclusion

The benefit of using list comprehension is that it allows one to write a few lines code which is easier to understand and easier to iterate into particular elements of the list. To convert into Custom Tuple Matrix, we saw there were various methods discussed above. In real life, matrices are used to represent statistical data such as infant mortality rate, population, and so on.

Updated on: 14-Aug-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements