The Wayback Machine - https://web.archive.org/web/20230316024404/https://www.geeksforgeeks.org/python-position-summation-in-list-of-tuples/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Position Summation in List of Tuples

Improve Article
Save Article
  • Last Updated : 14 Mar, 2023
Improve Article
Save Article

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




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

Time complexity: O(n), where n is the number of tuples in the list. The list comprehension iterates through each tuple in the list once.
Auxiliary space: O(1), as only a constant amount of space is used for variables such as test_list, res, and the print statements. 

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




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

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), since only a few variables are used to store the input and output lists.

Method 3: Using loop

Use a loop to iterate through each tuple in the list and adds up the values at the first and second positions of each tuple. The sums are then combined into a tuple and printed as the result.

Python3




# Python3 code to demonstrate
# position summation in list of tuples
# using loop
 
# 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 loop
sum_1 = 0
sum_2 = 0
for i in test_list:
    sum_1 += i[0]
    sum_2 += i[1]
res = (sum_1, sum_2)
 
# 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)

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), since only a few variables are used to store the input and output lists.

Method #4: Using the built-in function map() and sum()

This program calculates the sum of the first and second elements of each tuple in the input list test_list using map() and sum() functions, and then stores the results in the variables sum_1 and sum_2. Finally, it stores the tuple (sum_1, sum_2) in the variable res and prints it.

Python3




test_list = [(1, 6), (3, 4), (5, 8)]
 
# position Summation in List of Tuples using map() and sum()
sum_1 = sum(map(lambda x: x[0], test_list))
sum_2 = sum(map(lambda x: x[1], test_list))
res = (sum_1, sum_2)
 
# printing summation
print("The position summation of tuples  : " + str(res))

Output

The position summation of tuples  : (9, 18)

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(1) since we are only using a constant amount of extra space to store the variables sum_1, sum_2, and res

Method #6: Using numpy

This code first converts the list of tuples to a numpy array using the np.array() function. It then uses numpy’s sum() function to compute the position summation along the columns (i.e., axis=0). The result is a numpy array, so we convert it back to a tuple using the tuple() function before printing.

Python3




import numpy as np
 
# initializing list
test_list = [(1, 6), (3, 4), (5, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# convert list of tuples to numpy array
arr = np.array(test_list)
 
# compute position summation using numpy's sum() function
res = np.sum(arr, axis=0)
 
# print result
print("The position summation of tuples : " + str(tuple(res)))

OUTPUT: 
The original list is : [(1, 6), (3, 4), (5, 8)]
The position summation of tuples : (9, 18)

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), since we are creating a numpy array to store the input list of tuples.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!