Open In App

Python – Row-wise element Addition in Tuple Matrix

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python tuples, we can have a problem in which we need to perform Row-wise custom elements addition in Tuple matrix. This kind of problem can have application in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[(‘Gfg’, 3)], [(‘best’, 1)]] cus_eles = [1, 2] 
Output : [[(‘Gfg’, 3, 1)], [(‘best’, 1, 2)]] 

Input : test_list = [[(‘Gfg’, 6), (‘Gfg’, 3)]] cus_eles = [7] 
Output : [[(‘Gfg’, 6, 7), (‘Gfg’, 3, 7)]]

Method #1: Using enumerate() + nested list comprehension The combination of above methods can be used to solve this problem. In this, we perform the task of adding elements using nested list comprehension and index iteration by enumerate(). 

Python3




# Python3 code to demonstrate working of
# Row-wise element Addition in Tuple Matrix
# Using enumerate() + list comprehension
 
# initializing list
test_list = [[('Gfg', 3), ('is', 3)], [('best', 1)], [('for', 5), ('geeks', 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Custom eles
cus_eles = [6, 7, 8]
 
# Row-wise element Addition in Tuple Matrix
# Using enumerate() + list comprehension
res = [[sub + (cus_eles[idx], ) for sub in val] for idx, val in enumerate(test_list)]
 
# printing result
print("The matrix after row elements addition : " + str(res))


Output : 

The original list is : [[(‘Gfg’, 3), (‘is’, 3)], [(‘best’, 1)], [(‘for’, 5), (‘geeks’, 1)]] The matrix after row elements addition : [[(‘Gfg’, 3, 6), (‘is’, 3, 6)], [(‘best’, 1, 7)], [(‘for’, 5, 8), (‘geeks’, 1, 8)]]

  Method #2 : Using zip() + list comprehension The combination of above functions can be used to solve this problem. In this, we perform the task of combining new row elements with respective rows using zip(), rather than enumerate(). 

Python3




# Python3 code to demonstrate working of
# Row-wise element Addition in Tuple Matrix
# Using zip() + list comprehension
 
# initializing list
test_list = [[('Gfg', 3), ('is', 3)], [('best', 1)], [('for', 5), ('geeks', 1)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Custom eles
cus_eles = [6, 7, 8]
 
# Row-wise element Addition in Tuple Matrix
# Using zip() + list comprehension
res = [[(idx, val) for idx in key] for key,  val in zip(test_list, cus_eles)]
 
# printing result
print("The matrix after row elements addition : " + str(res))


Output : 

The original list is : [[(‘Gfg’, 3), (‘is’, 3)], [(‘best’, 1)], [(‘for’, 5), (‘geeks’, 1)]] The matrix after row elements addition : [[(‘Gfg’, 3, 6), (‘is’, 3, 6)], [(‘best’, 1, 7)], [(‘for’, 5, 8), (‘geeks’, 1, 8)]]

Approach#3: Using lambda

 In this solution, we use a for loop to iterate over each row of the input list and a lambda function with map to add the corresponding element from the customer elements list to each tuple.

Algorithm

1. Initialize an empty list result_list to store the updated tuples.
2. Iterate over each row row and its index i in the input list test_list.
3. Use a lambda function with map to add the corresponding element from the cus_eles list to each tuple in row.
4. Append the updated row to result_list.
5. Return the result_list.

Python3




test_list = [[('Gfg', 3)], [('best', 1)]]
cus_eles = [1, 2]
result_list = []
for i, row in enumerate(test_list):
    result_list.append(list(map(lambda x: x+(cus_eles[i],), row)))
print(result_list)


Output

[[('Gfg', 3, 1)], [('best', 1, 2)]]

Time complexity: O(n*m), where n is the number of rows in test_list and m is the maximum number of tuples in any row. This is because we need to iterate over each row and each tuple in the input list.

Space complexity: O(n*m), where n is the number of rows in test_list and m is the maximum number of tuples in any row. This is because we are creating a new list to store the updated tuples for each row.



Previous Article
Next Article

Similar Reads

Python Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20 Approach: Following are the steps: Sort each r
4 min read
Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
Find the number of negative numbers in a column-wise / row-wise sorted matrix M[][]. Suppose M has n rows and m columns. Example: Input: M = [-3, -2, -1, 1] [-2, 2, 3, 4] [4, 5, 7, 8] Output : 4 We have 4 negative numbers in this matrix We strongly recommend you to minimize your browser and try this yourself first. Naive Solution: Here's a naive, n
15+ min read
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in dimensions so it will return the higher dimension ten
3 min read
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 co
8 min read
Python Program to Find median in row wise sorted matrix
We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9Output : Median is 5If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9)Input: 1 3 4 2 5 6 7 8 9Output: Median is 5Recommended: Please solve it on “PRACTICE ” first
5 min read
heapq in Python to print all elements in sorted order from row and column wise sorted matrix
Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order. Examples: Input : mat= [[10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50]] Output : Elements of matrix in sorted order [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50] This problem h
2 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
three90RightbarBannerImg