Python – Summation in Dual element Records List

Sometimes, while working with Records list, we can have problem in which we perform the summation of dual tuple records and store it in list. This kind of application can occur over various domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension
This is one of the way to solve this problem. In this we perform summation of dual tuples in a list and iteration is performed inside comprehended list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Summation in Dual element Records List
# using list comprehension
  
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
  
# printing original lists
print("The original list is : " + str(test_list))
  
# Summation in Dual element Records List
# using list comprehension
res = [ele[0] + ele[1] for ele in test_list]
  
# printing result 
print ("Summation pairs in tuple list : " + str(res))

chevron_right


Output :

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]

 

Method #2 : Using reduce() + add()
This is yet another way to perform this task. In this, we iterate through the list and perform summation using reduce() and add() respectively.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Summation in Dual element Records List
# using reduce() + add()
from operator import add
from functools import reduce
  
# Initializing list
test_list = [(6, 7), (2, 4), (8, 9), (6, 2)]
  
# printing original lists
print("The original list is : " + str(test_list))
  
# Summation in Dual element Records List
# using reduce() + add()
res = [reduce(add, sub, 0) for sub in test_list]
  
# printing result 
print ("Summation pairs in tuple list : " + str(res))

chevron_right


Output :

The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]



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.