Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 from 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 Nested Loops with Set
This approach uses nested loops with a set to efficiently find triplets. We iterate through the list, and for each element, we look for pairs in the remaining elements that sum to the target value.
def find_triplets(numbers, target_sum):
result = []
for i in range(len(numbers) - 2):
seen = set()
remaining_sum = target_sum - numbers[i]
for j in range(i + 1, len(numbers)):
complement = remaining_sum - numbers[j]
if complement in seen:
triplet = (numbers[i], complement, numbers[j])
result.append(triplet)
seen.add(numbers[j])
return result
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
triplets = find_triplets(numbers, 40)
print("Required triplets:")
for triplet in triplets:
print(triplet)
Required triplets: (11, 15, 14) (11, 16, 13) (11, 17, 12) (12, 15, 13)
Using itertools.combinations
The itertools.combinations function generates all possible combinations of three elements. We then filter these combinations to find those that sum to our target value.
from itertools import combinations
def find_triplets_combinations(numbers, target_sum):
all_triplets = combinations(numbers, 3)
return [triplet for triplet in all_triplets if sum(triplet) == target_sum]
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
triplets = find_triplets_combinations(numbers, 40)
print("Required triplets:")
for triplet in triplets:
print(triplet)
Required triplets: (11, 12, 17) (11, 13, 16) (11, 14, 15) (12, 13, 15)
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Nested Loops with Set | O(n²) | O(n) | Large datasets |
| itertools.combinations | O(n³) | O(1) | Small datasets, readability |
Finding Triplets with Duplicates
If your list contains duplicate values and you want unique triplets, you can use a set to store results ?
def find_unique_triplets(numbers, target_sum):
unique_triplets = set()
for i in range(len(numbers) - 2):
seen = set()
remaining_sum = target_sum - numbers[i]
for j in range(i + 1, len(numbers)):
complement = remaining_sum - numbers[j]
if complement in seen:
triplet = tuple(sorted([numbers[i], complement, numbers[j]]))
unique_triplets.add(triplet)
seen.add(numbers[j])
return list(unique_triplets)
numbers = [1, 2, 3, 4, 5, 6, 3, 2]
triplets = find_unique_triplets(numbers, 10)
print("Unique triplets that sum to 10:")
for triplet in sorted(triplets):
print(triplet)
Unique triplets that sum to 10: (1, 3, 6) (1, 4, 5) (2, 2, 6) (2, 3, 5)
Conclusion
Use the nested loop approach for better performance with large datasets. Use itertools.combinations for simpler, more readable code with smaller datasets. For duplicate handling, sort triplets before adding to a set.
