Skip to content
Related Articles

Related Articles

Python – Pairs with Sum equal to K in tuple list

Improve Article
Save Article
  • Last Updated : 18 Oct, 2022
Improve Article
Save Article

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




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

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




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

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 #3 : Using sum() and list() methods

Python3




# 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 sum(list(ele)) == K:
        res.append(ele)
 
# printing result
print ("List after extracting pairs equal to K : " + str(res))

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
Related Articles

Start Your Coding Journey Now!