# Python program to find all possible pairs with given sum

• Difficulty Level : Easy
• Last Updated : 07 Jun, 2019

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.

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

Output:

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

Method #2 : Using `collections.Counter`

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

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

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.

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

Output:

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

Method #4 : `itertools.combinations` (Efficient method)

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

Output:

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

My Personal Notes arrow_drop_up