Home »
Python »
Python programs
Python program to perform row-wise element addition in Tuple matrix
Here, we have a tuple matrix and we need to perform a row-wise element addition operation on tuples of the tuple matrix using the Python program.
Submitted by Shivang Yadav, on September 11, 2021
While working with data structures, we might come across situations, where mixing them to extract some specific data is required. In such cases, options like cross paring and row-wise operations are helpful in extracting information.
In this problem, we will be performing a row-wise element addition operation on the tuple matrix. Now, before we move further, let’s understand some basics that are required in order to get a better understanding of the problem.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []
Example:
list = [3 ,1, 5, 7]
Performing row-wise element addition in Tuple matrix
We have a tuples matrix consisting of values and we need to perform addition which is row-wise on the matrix.
Input:
tupMat = [[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
addMat = [3 , 2]
Output:
[[(7, 2 ,6, 3), (4, 1, 5, 3)], (9, 2, 6, 2), (4, 5, 3, 2)]
We will be taking each row and adding values to it from another tuple. In Python, we have multiple methods and ways to perform the task.
Let's see some of these methods,
Method 1:
A method to solve the problem is by using nested list comprehension along with enumerate() method. We will use enumerate() to insert values to tuples of the tuple matrix.
# Python program to perform Row-wise element
# addition on tuple matrix
# Creating and printing tuple matrix
tupMat = [[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
additionVals = [3, 2]
print("Elements of Tuple matrix initially :" + str(tupMat))
# Performing Row-wise element addition operation on tuple matrix
addTupMat = [[sub + (additionVals[i], ) for sub in ele] for i, ele in enumerate(tupMat)]
# printing the added matrix
print("Elements of Tuple matrix after addition :" + str(addTupMat))
Output:
Elements of Tuple matrix initially :[[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
Elements of Tuple matrix after addition :[[(7, 2, 6, 3), (4, 1, 5, 3)], [(9, 2, 6, 2), (4, 5, 3, 2)]]
Method 2:
Another method to create the program for addition is by using the zip() method along with list comprehension to zip together the element to be added in with the tuples of the matrix. We will do this with each row to add elements to them.
# Python program to perform Row-wise element
# addition on tuple matrix
# Creating and printing tuple matrix
tupMat = [[(7, 2), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
additionVals = [3, 2]
print("Elements of Tuple matrix initially :" + str(tupMat))
# Performing Row-wise element addition operation on tuple matrix
addTupMat = [[(idx, val) for idx in key] for key, val in zip(tupMat, additionVals)]
# printing the added matrix
print("Elements of Tuple matrix after addition :" + str(addTupMat))
Output:
Elements of Tuple matrix initially :[[(7, 2), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
Elements of Tuple matrix after addition :[[((7, 2), 3), ((4, 1, 5), 3)], [((9, 2, 6), 2), ((4, 5, 3), 2)]]
Python Tuple Programs »