Python: Find all pairs in a list whose sum is equal to a target value
22. Find All Pairs in a List with Sum Equal to a Given Value
Write a Python program that finds all pairs of elements in a list whose sum is equal to a given value.
Sample Solution:
Python Code:
Sample Output:
Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Target value: 35 Find all pairs in the said list whose sum is equal to a target value: [{20, 15}, {16, 19}, {17, 18}, {17, 18}, {16, 19}, {20, 15}] Original list of numbers: [1, 2, 3, 4, 5] Target value: 5 Find all pairs in the said list whose sum is equal to a target value: [{1, 4}, {2, 3}, {2, 3}, {1, 4}]
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to find and print all unique pairs from a list that add up to a specified target sum.
- Write a Python program to use a two-pointer technique on a sorted list to identify pairs whose sum equals a given value.
- Write a Python program to implement a function that returns a list of tuple pairs that sum to the target value, ensuring no duplicates.
- Write a Python program to use dictionary mapping to find pairs that add up to a target sum in a single pass through the list.
Go to:
Previous: Unique words and frequency from a given list of strings.
Next: Longest common prefix of all the strings.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.