Open In App

Python – Row-wise element Addition in Tuple Matrix

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.



Last Updated : 18 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads