Python – Tuple Matrix Columns Summation

Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[(4, 5), (1, 2)], [(2, 4), (4, 6)]]
Output : [(6, 9), (5, 8)]

Input : test_list = [[(4, 5), (1, 2), (6, 7)]]
Output : [(4, 5), (1, 2), (6, 7)]

Method #1 : Using list comprehension + zip() + sum()
The combination of above functions can be used to solve this problem. In this, we perform the task of sum using sum() and zip() is used to perform column wise pairing of all elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Tuple Matrix Columns Summation
# Using list comprehension + zip() + sum()
  
# initializing lists
test_list = [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Tuple Matrix Columns Summation
# Using list comprehension + zip() + sum()
res = [tuple(sum(ele) for ele in zip(*i)) for i in zip(*test_list)]
  
# printing result 
print("Tuple matrix columns summation : " + str(res))

chevron_right


Output :



The original list is : [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
Tuple matrix columns summation : [(9, 9), (11, 13)]

 

Method #2 : Using map() + list comprehension + zip()
The combination of above functions can be used to solve this problem. In this, we perform the task of extension of sum() using map() and rest of the functionalities are performed similar to above method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Tuple Matrix Columns Summation
# Using map() + list comprehension + zip()
  
# initializing lists
test_list = [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Tuple Matrix Columns Summation
# Using map() + list comprehension + zip()
res = [tuple(map(sum, zip(*ele))) for ele in zip(*test_list)]
  
# printing result 
print("Tuple matrix columns summation : " + str(res))

chevron_right


Output :

The original list is : [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
Tuple matrix columns summation : [(9, 9), (11, 13)]

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.