# Python – Closest Pair to Kth index element in Tuple

Sometimes, while working with Python records, we can have a problem in which we need to find the tuple nearest to certain tuple, query on a particular index. This kind of problem can have application in data domains such as web development. Let’s discuss certain ways in which this task can be performed.

Input :
test_list = [(3, 4), (78, 76), (2, 3), (9, 8), (19, 23)]
tup = (17, 23)
K = 2

Output : (19, 23)
Input :
test_list = [(3, 4, 9), (5, 6, 7)]
tup = (1, 2, 5)
K = 3
Output : (5, 6, 7)

Method #1 : Using enumerate() + loop
The combination of above functions offer brute force way to solve this problem. In this, we use enumerate() to monitor index and abs() to keep the minimum difference updated checked for each element over a loop.

 # Python3 code to demonstrate working of  # Closest Pair to Kth index element in Tuple # Using enumerate() + loop    # initializing list test_list = [(3, 4), (78, 76), (2, 3), (9, 8), (19, 23)]    # printing original list print("The original list is : " + str(test_list))    # initializing tuple tup = (17, 23)    # initializing K  K = 1    # Closest Pair to Kth index element in Tuple # Using enumerate() + loop min_dif, res = 999999999, None for idx, val in enumerate(test_list):     dif = abs(tup[K - 1] - val[K - 1])     if dif < min_dif:         min_dif, res = dif, idx    # printing result  print("The nearest tuple to Kth index element is : " + str(test_list[res]))

Output :

The original list is : [(3, 4), (78, 76), (2, 3), (9, 8), (19, 23)]
The nearest tuple to Kth index element is : (19, 23)

Method #2 : Using min() + lambda
The combination of above functions offer shorthand to solve this problem. In this, we use min() to find minimum element difference and lambda function is used to perform iterations and computations.

 # Python3 code to demonstrate working of  # Closest Pair to Kth index element in Tuple # Using min() + lambda    # initializing list test_list = [(3, 4), (78, 76), (2, 3), (9, 8), (19, 23)]    # printing original list print("The original list is : " + str(test_list))    # initializing tuple tup = (17, 23)    # initializing K  K = 1    # Closest Pair to Kth index element in Tuple # Using min() + lambda res = min(range(len(test_list)), key = lambda sub: abs(test_list[sub][K - 1] - tup[K - 1]))    # printing result  print("The nearest tuple to Kth index element is : " + str(test_list[res]))

Output :

The original list is : [(3, 4), (78, 76), (2, 3), (9, 8), (19, 23)]
The nearest tuple to Kth index element is : (19, 23)

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.