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
# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using list comprehension + combinations
from itertools import combinations
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
# Printing original list
print("The original list : " + str(test_list))
# Summation combination in tuple lists
# Using list comprehension + combinations
res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)]
# Printing result
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
# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using list comprehension + zip() + operator.add + combinations()
from itertools import combinations
import operator
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
# Printing original list
print("The original list : " + str(test_list))
# Summation combination in tuple lists
# Using list comprehension + zip() + operator.add + combinations()
res = [(operator.add(*a), operator.add(*b))
for a, b in (zip(y, x) for x, y in combinations(test_list, 2))]
# Printing result
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
# Python3 code to demonstrate working of
# Summation combination in tuple lists
# Using nested for loops
# Initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
# Printing original list
print("The original list : " + str(test_list))
# Summation combination in tuple lists
# Using nested for loops
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]))
# Printing result
print("The Summation combinations are : " + str(res))
OutputThe 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
# Input list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10)]
# Printing input list for understanding
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)))
# Printing the resultant list
print("The Summation combinations are : " + str(res))
OutputThe 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)
Similar Reads
Python | Combining tuples in list of tuples Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways
7 min read
Python - Join Tuples to Integers in Tuple List Sometimes, while working with Python records, we can have a problem in which we need to concatenate all the elements, in order, to convert elements in tuples in List to integer. This kind of problem can have applications in many domains such as day-day and competitive programming. Let's discuss cert
5 min read
Flatten tuple of List to tuple - Python The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Combinations of Elements till size N in List - Python We are given a list and our task is to generate all possible combinations of its elements up to a given size N, including all lengths from 1 to N. For example, if we have a = [1, 2, 3] and N = 2 then the output should be [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3)], where we generate all subsets of si
3 min read
Python | Count tuples occurrence in list of tuples Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using It
5 min read