Python | Summation of tuples in list
Sometimes, while working with records, we can have a problem in which we need to find the cummulative sum of all the values that are present in tuples. This can have application in cases in which we deal with a lot of records data. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using sum() + map()
Combination of above functions can be used to solve this particular problem. In this the task of summation is performed by sum(), and applying the summation functionality to each element in tuple list is performed by map().
# Python3 code to demonstrate working of # Summation of tuples in list # using sum() + map() # initialize list of tuple test_list = [(1, 3), (5, 6, 7), (2, 6)] # printing original tuples list print("The original list : " + str(test_list)) # Summation of tuples in list # using sum() + map() res = sum(map(sum, test_list)) # printing result print("The summation of all tuple elements are : " + str(res)) |
The original list : [(1, 3), (5, 6, 7), (2, 6)] The summation of all tuple elements are : 30
Method #2 : Using sum() + izip()
The combination of above functions can be used to perform this particular task. In this, we perform the task of map() using izip(). It helps to club all the elements for summation by sum(). Works only for single element tuple and only with Python2.
# Python3 code to demonstrate working of # Summation of tuples in list # using sum() + izip() from itertools import izip # initialize list of tuple test_list = [(1, ), (5, ), (2, )] # printing original tuples list print("The original list : " + str(test_list)) # Summation of tuples in list # using sum() + map() res = sum(*izip(*test_list)) # printing result print("The summation of all tuple elements are : " + str(res)) |
The original list : [(1, ), (5, ), (2, )] The summation of all tuple elements are : 8
Recommended Posts:
- Python | Summation of two list of tuples
- Python | Position Summation in List of Tuples
- Python | Column summation of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Convert string tuples to list tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Pair summation of list elements
- Python | Alternate element summation in list
- Python | Grouped summation of tuple list
- Python - Summation of float string list
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.

