Python – Pairs with Sum equal to K in tuple list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.