Python – Combinations of sum with tuples in tuple list
Last Updated :
05 May, 2023
Sometimes, while working with data, we can have a problem in which we need to perform tuple addition among all the tuples in list. This can have applications in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1: Using combinations() + list comprehension
This problem can be solved using combinations of the above functions. In this, we use combinations() to generate all possible combinations among tuples and list comprehension is used to feed addition logic.
Python3
from itertools import combinations
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2 )]
print ( "The Summation combinations are : " + str (res))
|
Output :
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]
Time complexity: O(n^2) where n is the length of the input list.
Auxiliary space: O(n) where n is the length of the input list.
Method #2 : Using list comprehension + zip() + operator.add + combinations()
The combinations of the above methods can also solve this problem. In this, we perform the task of addition using add() and the like indexed elements are linked using zip() function.
Python3
from itertools import combinations
import operator
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = [(operator.add( * a), operator.add( * b))
for a, b in ( zip (y, x) for x, y in combinations(test_list, 2 ))]
print ( "The Summation combinations are : " + str (res))
|
Output :
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the list comprehension + zip() + operator.add + combinations() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3: Using nested for loops
Use nested for loops to iterate over the list and add the elements of each tuple to find the summation combination
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = []
for i in range ( len (test_list)):
for j in range (i + 1 , len (test_list)):
res.append((test_list[i][ 0 ] + test_list[j][ 0 ],
test_list[i][ 1 ] + test_list[j][ 1 ]))
print ( "The Summation combinations are : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]
Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n^2), where n is the length of the input list.
Method #4: Using itertools.combinations() + map() + lambda function
- Import itertools module’s combinations() function, which generates all possible combinations of elements of the input iterable
- Apply map() function with a lambda function to calculate the sum of each tuple in the generated combinations
- Store the resulting tuples in a list.
Python3
import itertools
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = list ( map ( lambda x: (x[ 0 ][ 0 ] + x[ 1 ][ 0 ], x[ 0 ][ 1 ] +
x[ 1 ][ 1 ]), itertools.combinations(test_list, 2 )))
print ( "The Summation combinations are : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Summation combinations are : [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]
Time complexity: O(n^2) (because we need to generate all possible combinations of size 2 from n elements)
Auxiliary space: O(n^2) (because we need to store all the generated tuples in a list)
Please Login to comment...