• 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
  • Ruby
  • 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 Selenium to Check if Element Exists in Python
  • 2.  Convert True to 1 in Python
  • 3.  Convert String into Tuple in Python
  • 4.  Remove Empty Lists from List in Python
  • 5.  Python Get Operating System Information with os and platform Modules
  • 6.  Using Python to Split a Number into Digits
  • 7.  Create Unique List from List in Python
  • 8.  pandas groupby size – Get Number of Elements after Grouping DataFrame
  • 9.  Count Unique Values in pandas DataFrame
  • 10.  Get Week Number from Date 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy