Related Articles

Related Articles

Python – Row-wise element Addition in Tuple Matrix
  • Last Updated : 30 Dec, 2020

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


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)]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :