Open In App

Python – Combinations of sum with tuples in tuple list

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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

  1. Import itertools module’s combinations() function, which generates all possible combinations of elements of the input iterable
  2. Apply map() function with a lambda function to calculate the sum of each tuple in the generated combinations
  3. 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))


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)



Similar Reads

itertools.combinations() module in Python to print all possible combinations
Given an array of size n, generate and print all possible combinations of r elements in array. Examples: Input : arr[] = [1, 2, 3, 4], r = 2 Output : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]Recommended: Please try your approach on {IDE} first, before moving on to the solution. This problem has existing recursive solution please refer Print
2 min read
Python program to create a list of tuples from given list having number and its cube in each tuple
Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number. Example: Input: list = [1, 2, 3] Output: [(1, 1), (2, 8), (3, 27)] Input: list = [9, 5, 6] Output: [(9, 729), (5, 125), (6, 216)] Method #1 : Using pow() function.We can use list compreh
5 min read
Python Program to Convert Tuple Value List to List of Tuples
Given a dictionary with values as a tuple list, convert it to a key-mapped list of tuples. Input : test_dict = {'Gfg' : [(5, ), (6, )], 'is' : [(5, )], 'best' :[(7, )]} Output : [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)] Explanation : Keys grouped with values.Convert Tuple Value List to List of Tuples Using loop + * operator + items() This is
8 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 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 Iteration C/C++ Code # Python code to count unique #
5 min read
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 in which this can be performed. Method #1: Using
7 min read
Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read