Sometimes, while working with data, we can have a problem in which we need to find the sum of pairs of tuple list. And specifically the sum that is equal to K. This kind of problem can be important in web development and competitive programming. Lets discuss certain ways in which this task can be performed.

**Method #1 : Using loop**

This can be solved using loop. This is brute way in which this task is performed. In this, we iterate the list for pair summation and retain whose sum is K.

`# Python3 code to demonstrate ` `# Pairs with Sum equal to K in tuple list ` `# using loop ` ` ` `# Initializing list ` `test_list ` `=` `[(` `4` `, ` `5` `), (` `6` `, ` `7` `), (` `3` `, ` `6` `), (` `1` `, ` `2` `), (` `1` `, ` `8` `)] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Initializing K ` `K ` `=` `9` ` ` `# Pairs with Sum equal to K in tuple list ` `# using loop ` `res ` `=` `[] ` `for` `ele ` `in` `test_list: ` ` ` `if` `ele[` `0` `] ` `+` `ele[` `1` `] ` `=` `=` `K: ` ` ` `res.append(ele) ` ` ` `# printing result ` `print` `(` `"List after extracting pairs equal to K : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [(4, 5), (6, 7), (3, 6), (1, 2), (1, 8)] List after extracting pairs equal to K : [(4, 5), (3, 6), (1, 8)]

**Method #2 : Using list comprehension**

This is yet another way in which this task can be performed. In this, we extract the elements in similar method as above, the difference is that we perform this task as shorthand and in one line.

`# Python3 code to demonstrate ` `# Pairs with Sum equal to K in tuple list ` `# using list comprehension ` ` ` `# Initializing list ` `test_list ` `=` `[(` `4` `, ` `5` `), (` `6` `, ` `7` `), (` `3` `, ` `6` `), (` `1` `, ` `2` `), (` `1` `, ` `8` `)] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Initializing K ` `K ` `=` `9` ` ` `# Pairs with Sum equal to K in tuple list ` `# using list comprehension ` `res ` `=` `[(ele[` `0` `], ele[` `1` `]) ` `for` `ele ` `in` `test_list ` `if` `ele[` `0` `] ` `+` `ele[` `1` `] ` `=` `=` `K] ` ` ` `# printing result ` `print` `(` `"List after extracting pairs equal to K : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [(4, 5), (6, 7), (3, 6), (1, 2), (1, 8)] List after extracting pairs equal to K : [(4, 5), (3, 6), (1, 8)]

## Recommended Posts:

- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Maximum of Product Pairs in Tuple List
- Python | Pair and combine nested list to tuple list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert mixed data types tuple list to string list
- Python | Convert List of Dictionary to Tuple list
- Python - Convert Tuple value list to List of tuples
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python | Find Maximum difference between tuple pairs
- Python - Similar index pairs in Tuple lists
- Python - Convert Matrix to overlapping Tuple Pairs

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.