Python | Summation of two list of tuples
Last Updated :
21 Apr, 2023
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [(x[ 0 ] + y[ 0 ], x[ 1 ] + y[ 1 ]) for x, y in zip (test_list1, test_list2)]
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [ tuple ( map ( sum , zip (a, b))) for a, b in zip (test_list1, test_list2)]
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
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [ tuple (i) for i in np.add(test_list1, test_list2)]
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 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 = []
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)
|
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 )]
res = list ( map ( lambda x, y: (x[ 0 ] + y[ 0 ], x[ 1 ] + y[ 1 ]), test_list1, test_list2))
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
- Two lists of tuples named test_list1 and test_list2 are initialized with some values.
- The starmap() and zip() functions from the itertools module are imported.
- The original lists, test_list1 and test_list2, are printed using the print() function.
- The starmap() function is used to iterate over two lists of tuples simultaneously, by zipping the two lists using the zip() function.
- 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.
- The resulting tuples are collected into a list using the list() function, and stored in the variable res.
- The final result is printed using the print() function.
Python3
from itertools import starmap
test_list1 = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 )]
test_list2 = [( 5 , 4 ), ( 8 , 10 ), ( 8 , 14 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = list (starmap( lambda x, y: tuple ( map ( sum , zip (x, y))), zip (test_list1, test_list2)))
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
Share your thoughts in the comments
Please Login to comment...