
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find all triplets in a list with given sum in Python
In a list of numbers we want to find out which three elements can join to give a certain sum. We call it a triplet. And in the list there can be many such triplets. For example, the sum 10 can be generated form numbers 1,6,3 as well as 1,5,4. In this article we will see how to find out all such triplets from a given list of numbers.
Using range and temp variables
This is the traditional approach in which we will create temporary variables. These variables will hold the elements from the list and check if their sum is equating to the required value. Then it will keep accumulating such variables into the final result set.
Example
def SumTriplets(listA, sum): trpltcnt = 0 res = [] for i in range(0, len(listA) - 1): s = set() tmp = [] # Adding first element tmp.append(listA[i]) current_sum = sum - listA[i] for j in range(i + 1, len(listA)): if (current_sum - listA[j]) in s: trpltcnt += 1 # Adding second element tmp.append(listA[j]) # Adding third element tmp.append(current_sum - listA[j]) # Appending tuple to the final list res.append(tuple(tmp)) tmp.pop(2) tmp.pop(1) s.add(listA[j]) return res listA = [11,12,13,14,15,16,17,18,19,20] print("Required triplets:\n",SumTriplets(listA, 40))
Output
Running the above code gives us the following result −
Required triplets: [(11, 15, 14), (11, 16, 13), (11, 17, 12), (12, 15, 13)]
Example
from itertools import combinations listA = [11,12,13,14,15,16,17,18,19,20] def fsum(val): return sum(val) == 40 res = list(filter(fsum,list(combinations(listA, 3)))) print("Required triplets:\n",res)
Output
Running the above code gives us the following result −
Required triplets: [(11, 12, 17), (11, 13, 16), (11, 14, 15), (12, 13, 15)]
- Related Articles
- Print all triplets with given sum in C++
- Find all triplets with zero sum in C++
- JavaScript Program to Find all triplets with zero sum
- All unique triplets that sum up to a given value in C++
- Python - Ways to create triplets from given list
- Program to find kpr sum for all queries for a given list of numbers in Python
- Python program to find all the Combinations in a list with the given condition
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Find all the pairs with given sum in a BST in C++
- Python – Strings with all given List characters
- JavaScript Program to Count triplets with sum smaller than a given value
- Find pairs with given sum in doubly linked list in C++
- Python program to find all the Combinations in the list with the given condition
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Python Program – Strings with all given List characters

Advertisements