Open In App

Python | Summation of two list of tuples

Last Updated : 21 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we need to perform cross-summation of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + zip() The combination of above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the summation across lists is performed with help of zip(). 

Python3




# Python3 code to demonstrate working of
# Summation of two list of tuples
# using list comprehension + zip()
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Summation of two list of tuples
# using list comprehension + zip()
res = [(x[0] + y[0], x[1] + y[1]) for x, y in zip(test_list1, test_list2)]
 
# printing result
print("The Summation across lists is : " + str(res))


Output

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time complexity: O(n), where n is the length of the input lists
Auxiliary space: O(n), where n is the length of the input lists.

Method #2 : Using sum() + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that summation is performed by inbuilt function and extending logic to each element is done by map(). 

Python3




# Python3 code to demonstrate working of
# Summation of two list of tuples
# using sum() + zip() + map()
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Summation of two list of tuples
# using sum() + zip() + map()
res = [tuple(map(sum, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The Summation across lists is : " + str(res))


Output

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time complexity: O(n), where n is the length of the lists. 
Auxiliary space: O(n), as the resulting list res has the same length as the input lists.

Method #3: Using numpy

Note: Install numpy module using command “pip install numpy”

The numpy library in python provides a function called numpy.add() which can be used to perform the cross summation of two lists of tuples.

Python3




import numpy as np
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Summation of two list of tuples using numpy
res = [tuple(i) for i in np.add(test_list1, test_list2)]
 
# printing result
print("The Summation across lists is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time complexity: O(n) where n is the size of the lists. 
Auxiliary Space: O(n)

Method #4: Using  a for loop:

Python3




test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
res = []
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
for x, y in zip(test_list1, test_list2):
    res.append((x[0] + y[0], x[1] + y[1]))
print("The Summation across lists is :", res)
#This code is contributed by Jyothi pinjala.


Output

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time complexity: O(n) 
Auxiliary Space: O(n)

Method #5: Using a lambda function with map() and zip()

Python3




test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# using lambda function with map() and zip()
# lambda function takes two arguments, x and y
# zip() function combines the elements of test_list1 and test_list2
# map() applies the lambda function to each pair of tuples in the zipped list
res = list(map(lambda x, y: (x[0] + y[0], x[1] + y[1]), test_list1, test_list2))
 
# printing the result
print("The Summation across lists is : " + str(res))


Output

The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time Complexity: O(n), where n is the length of the input lists test_list1 and test_list2. 
Auxiliary Space: O(n), where n is the length of the input lists test_list1 and test_list2. 

Method #11: Using the itertools module’s starmap() function

  1. Two lists of tuples named test_list1 and test_list2 are initialized with some values.
  2. The starmap() and zip() functions from the itertools module are imported.
  3. The original lists, test_list1 and test_list2, are printed using the print() function.
  4. The starmap() function is used to iterate over two lists of tuples simultaneously, by zipping the two lists using the zip() function.
  5. For each pair of tuples, the zip() function is used to combine the elements of the tuples into a single tuple, and the map() function is used to apply the sum() function to the zipped tuple to get a new tuple with the sum of the corresponding elements of the original tuples.
  6. The resulting tuples are collected into a list using the list() function, and stored in the variable res.
  7. The final result is printed using the print() function.

Python3




from itertools import starmap
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Summation of two list of tuples
# using the starmap() function from itertools
res = list(starmap(lambda x, y: tuple(map(sum, zip(x, y))), zip(test_list1, test_list2)))
 
# printing result
print("The Summation across lists is : " + str(res))


Output

The original list 1 : [(2, 4), (6, 7), (5, 1)]
The original list 2 : [(5, 4), (8, 10), (8, 14)]
The Summation across lists is : [(7, 8), (14, 17), (13, 15)]

Time complexity: O(n), where n is the length of the input lists
Auxiliary space: O(n), for storing the output list



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads