Find Closest Value in List in Python (2 Examples)

 

Hi! This short tutorial will show you how to determine the nearest value in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example List

Here, we will create the example Python list of integers that we will use in the examples in this tutorial, and define the target value whose nearest match in the list we will find.

Therefore, in your preferred Python IDE, run the code below to create the example list:

my_list = [5, 12, 7, 3, 20]
 
print(my_list)
 
# [5, 12, 7, 3, 20]
 
print(type(my_list))
 
# <class 'list'>
 
target_value = 10

Now that we have created the example list, we will consider two ways to get the value in the list that is the closest to the target value.

 

Example 1: Get Closest Value in List Using min() function

In this first example, we will use the min() function to return the closest value in the list:

closest_value = min(my_list, key=lambda x: abs(target_value - x))
 
print(closest_value)
 
# 12
 
print(type(closest_value))
 
# <class 'int'>

In the above example, the line closest_value = min( my_list, key = lambda x : abs(target_value - x) ) finds the closest value to the target in my_list using the min() function.

It uses a lambda function as the key argument to calculate the absolute difference between the target value and each element in the list using abs() function.

The min() function then returns the element with the minimum difference, which is assigned to closest_value.

Finally, the value of closest_value is printed using the print() function.
 

Example 2: Get Closest Value in List Using sorted() Function

In this second example, we will use the sorted() function to return the nearest value in the list:

sorted_list = sorted(my_list, key=lambda x: abs(target_value - x))
 
closest_value = sorted_list[0]
 
print(closest_value)
 
# 12
 
print(type(closest_value))
 
# <class 'int'>

Here, we first sort the list in ascending order based on the absolute difference between each element and a target value.

The sorting is achieved using the sorted() function with a lambda function as the key argument.

The lambda function calculates the absolute difference between the target value and each element in the list.

After sorting, the first element of the sorted list is assigned to closest_value.

Finally, the value of closest_value is printed.
 

Video, Further Resources & Summary

Do you need more explanations on how to find the closest value in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to find the closest value in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python lists tutorials on Statistics Globe, starting with these ones:

This post has shown, using two examples, how to find the closest value in a list in Python. Your use case will determine which method to adopt.

I do hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top