Signup/Sign In
PUBLISHED ON: FEBRUARY 18, 2022

Python Program to Find Closest Pair to Kth index element in Tuple

In this tutorial, you will learn to write a Python program to find the closest pair to the element at the Kth index in a tuple. A tuple is a data structure that stores a sequence of data. From a given list of tuples, we have to find the pair which is the closest to a certain tuple from the Kth index element in the tuple.

Look at the examples to understand the input and output format.

Input:

list=[(10, 3), (6, 3), (3, 2),(1, 5)]

tuple=(6, 2)

K= 1

Output: (6, 3)

Input:

list=[(30, 34), (2, 6), (10, 15),(11, 4, (8, 9))]

tuple=(7, 15)

K= 2

Output: (10, 15)

To solve this problem in python, we can use the following approaches-

  1. using a loop and enumerate() method
  2. using lambda function and min() method

We will discuss both approaches in detail below.

Approach 1: loop and enumerate()

In this approach, we use enumerate() method to track the Kth index for finding the closest element from the tuples. To find the closest pair, the difference between the given tuple and the closest pair should be as minimum as possible which can be done by using abs().To update the difference we will use a loop to iterate over each element.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a list with tuple values

Step 2- Initialise a tuple pair and K

Step 3- Set a variable with a very high value for storing the minimum difference

Step 4- Run a loop to iterate over the list

Step 5- Find the difference between the tuple pair and the element in the list

Step 6- For which the difference is minimum, store the index of that pair

Step 7- Use that index to print the tuple pair

Step 8- This will be the final result

Python Program 1

In this program, we have to find a tuple from a given list of tuples, which is closest to a certain tuple where K is a query on a particular index.


# Closest Pair to Kth index element in Tuple
# Using enumerate() and loop
  
# initialize list
li = [(3, 1), (9, 10), (1, 6), (10, 13), (45, 56)]
  
# printing original list
print("List: ",li)
  
# initialize tuple and K
tup, K = (10, 12), 1
    
# finding closest Pair 
min_dif, res = 999999999, None
for i, val in enumerate(li):
    dif = abs(tup[K - 1] - val[K - 1])
    if dif < min_dif:
        min_dif, res = dif, i
  
# printing result 
print("The nearest tuple to Kth index element is: " ,li[res])


List: [(3, 1), (9, 10), (1, 6), (10, 13), (45, 56)]
The nearest tuple to Kth index element is: (10, 13)

Approach 2: lambda function and min()

In this approach, the min() method will be used to find the minimum difference between the element and the tuple. The lambda function is used to perform iterations for the list of tuples. A lambda function is a way of declaring an anonymous function in Python.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a list with tuple values

Step 2- Initialise a tuple pair and K

Step 3- Declare a variable for storing the index of the closest pair

Step 4- Find the closest pair by using a combination of methods and lambda function

Step 5- Define lambda function to iterate over every tuple in list

Step 6- Print the tuple present at the index value stored in the variable

Step 7- This will be the final result

Python Program 2

In this program, we have defined a lambda function that will iterate over the tuples and fin the minimum difference between the tuple pair and the element in the list. To find the length of the list we have used len() method. It returns the size of a list.

# Closest Pair to Kth index element in Tuple
# Using enumerate() and loop
  
# initialize list
li = [(3, 1), (9, 10), (1, 6), (10, 13), (45, 56)]
  
# printing original list
print("List: ",li)
  
# initialize tuple and K
tup, K = (2,10), 1
    
# finding closest Pair 
result= min(range(len(li)), key= lambda sub: abs(li[sub][K - 1] - tup[K - 1]))
  
# printing result 
print("The nearest tuple to Kth index element is: " ,li[result])


List: [(3, 1), (9, 10), (1, 6), (10, 13), (45, 56)]
The nearest tuple to Kth index element is: (3, 1)

Conclusion

In this tutorial, we have discussed two different methods for finding the closest pair from a given list of tuples in python. In the first approach, we have used loop and enumerate() method. In the second approach, we have used the lambda function and the min() method.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.