Python | Position Summation in List of Tuples
Sometimes the data that we receive, is in the form of tuples having data from various sources and we can usually have a use case in which we require to process the sum of each index of tuple for cumulation. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension
This is the most naive method to perform this particular task, in this method we compute the summation of each index of all the possible indices of the tuple.
# Python3 code to demonstrate # position summation in list of tuples # using list comprehension # initializing list test_list = [(1, 6), (3, 4), (5, 8)] # printing original list print ("The original list is : " + str(test_list)) # position Summation in List of Tuples # using list comprehension res = sum(i[0] for i in test_list), sum(i[1] for i in test_list) # printing summation print ("The position summation of tuples : " + str(res)) |
Output :
The original list is : [(1, 6), (3, 4), (5, 8)] The position summation of tuples : (9, 18)
Method #2 : Using zip() + sum()
This is the most elegant and pythonic way to perform this particular task. In this we combine all the indices of the element using zip() and the performance of summation using sum function.
# Python3 code to demonstrate # position summation in list of tuples # using zip() + sum() # initializing list test_list = [(1, 6), (3, 4), (5, 8)] # printing original list print ("The original list is : " + str(test_list)) # position Summation in List of Tuples # using zip() + sum() res = [sum(i) for i in zip(*test_list)] # printing summation print ("The position summation of tuples : " + str(res)) |
Output :
The original list is : [(1, 6), (3, 4), (5, 8)] The position summation of tuples : [9, 18]
Recommended Posts:
- Python | Column summation of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Count tuples occurrence in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a 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 | Grouped summation of tuple list
- Python | Alternate element summation in list
- Python | Summation of list as tuple attribute
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of list
- Python | Convert list of tuples into 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.



