• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Find Closest Value in List

Using Python to Find Closest Value in List

May 9, 2022 Leave a Comment

To find the closest value to a given number in a list of numbers, the easiest way is to use the Python min() function with a lambda function.

lst = [5, 6, 10, 15, 21, 14, -1]
n = 13

closest = min(lst, key=lambda x: abs(x-n))

print(closest)

#Output:
14

You can also use the numpy module to get the closest value to another number in a list.

import numpy as np

lst = [5, 6, 10, 15, 21, 14, -1]
n = 13

np_lst = np.asarray(lst)
idx = (np.abs(np_lst - n)).argmin()

closest = lst[idx]

print(closest)

#Output:
14

When working with collections of data in Python, the ability to get different statistics and information from your data is valuable.

One piece of information which we can get in Python is the closest value of a number in a list.

We can easily find the closest value in a list of a number using the Python min() function and the Python abs() function.

Below is an example showing how you can get the closest value in a list using Python.

lst = [5, 6, 10, 15, 21, 14, -1]
n = 13

closest = min(lst, key=lambda x: abs(x-n))

print(closest)

#Output:
14

Putting this in a function, you can find the closest value of a list with a function in Python as shown below.

list_of_numbers = [5, 6, 10, 15, 21, 14, -1]
num = 13

def closestValue(lst,n):
    return min(lst, key=lambda x: abs(x-n))

print(closestValue(list_of_numbers,num))

#Output:
14

Using numpy Module to Find Closest Value in List in Python

You can also use numpy to find the closest value in a list using Python.

First, we need to convert our list to a numpy array and then we can use abs() function and argmin() function.

Below is how you can use numpy to find the closest value in a list.

import numpy as np

lst = [5, 6, 10, 15, 21, 14, -1]
n = 13

np_lst = np.asarray(lst)
idx = (np.abs(np_lst - n)).argmin()

closest = lst[idx]

#Output:
14

Putting this in a function, you can find the closest value of a list with a function as shown below.

import numpy as np

lst = [5, 6, 10, 15, 21, 14, -1]
n = 13

def closestValue(lst,n):
    lst = np.asarray(lst)
    idx = (np.abs(lst - n)).argmin()
    return lst[idx]

print(closestValue(lst,n)

#Output:
14

Hopefully this article has been useful for you to learn how to find the closest value to a given value in a list using Python.

Other Articles You'll Also Like:

  • 1.  Using Lambda Expression with min() in Python
  • 2.  Python Print List – Printing Elements of List to the Console
  • 3.  Using pandas sample() to Generate a Random Sample of a DataFrame
  • 4.  Remove Extension from Filename in Python
  • 5.  Check if Word is Palindrome Using Recursion with Python
  • 6.  Python power function – Exponentiate Numbers with math.pow()
  • 7.  Python Remove First Element from List
  • 8.  Using Python to Search File for String
  • 9.  How to Check if a Dictionary is Empty in Python
  • 10.  PROC PHREG Equivalent in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy