Python program to find all possible pairs with given sum

Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K.

Examples:

Input : lst =[1, 5, 3, 7, 9]
        K = 12
Output : [(5, 7), (3, 9)]

Input : lst = [2, 1, 5, 7, -1, 4]
        K = 6
Output : [(2, 4), (1, 5), (7, -1)]

 
Method #1 : Pythonic Naive

This is a naive approach to the above problem. First, we take an empty list ‘res’ and start a loop and traverse each element of the given list of integers. In each iteration, pop the element, store it in ‘num’, find remaining difference for sum K, and check if the difference exists in the given list or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all pairs in 
# a list of integers with given sum 
  
def findPairs(lst, K): 
    res = []
    while lst:
        num = lst.pop()
        diff = K - num
        if diff in lst:
            res.append((diff, num))
          
    res.reverse()
    return res
      
# Driver code
lst = [1, 5, 3, 7, 9]
K = 12
print(findPairs(lst, K))

chevron_right


Output:

[(5, 7), (3, 9)]

 
Method #2 : Using collections.Counter

This approach follows the same method as discussed above using collections.Counter.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all pairs in 
# a list of integers with given sum 
from collections import Counter
  
def findPairs(lst, K): 
    res = []
    count = Counter(lst)
  
    for x in lst:
        y = K - x
        if (x != y and count[y]) or (x == y and count[y] > 1):
            res.append((x, y)) 
            count.subtract((x, y))
              
    return res
      
# Driver code
lst = [1, 5, 3, 7, 9]
K = 12
print(findPairs(lst, K))

chevron_right


Output:

[(5, 7), (3, 9)]

 
Method #3 : itertools.combinations (Naive method)

This is a naive approach to use itertools.combinations. We use a for loop to traverse through each combination and find out the desired one.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all pairs in 
# a list of integers with given sum 
  
from itertools import combinations
  
def findPairs(lst, K):   
    res = []
    for var in combinations(lst, 2):
        if var[0] + var[1] == K:
            res.append((var[0], var[1]))
          
    return res
      
# Driver code
lst = [1, 5, 3, 7, 9]
K = 12
print(findPairs(lst, K))

chevron_right


Output:

[(5, 7), (3, 9)]

 
Method #4 : itertools.combinations (Efficient method)

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all pairs in 
# a list of integers with given sum 
from itertools import combinations
  
def findPairs(lst, K):
      
    return [pair for pair in combinations(lst, 2) if sum(pair) == K]
      
# Driver code
lst = [1, 5, 3, 7, 9]
K = 12
print(findPairs(lst, K))

chevron_right


Output:

[(5, 7), (3, 9)]


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.