Change language

# Python | Find the closest key in the dictionary

| |

Method # 1: Using a comprehension list + ` keys () ` + lambda
A combination of the above functions can be used to perform the specific task of finding the closest key in a dictionary. The keys function can be used to access keys from a dictionary, the lambda function can be used to formulate logic and comprehension of a list to apply it all to the whole list.

 ` # Python3 code to demonstrate how it works ` ` # Closest key in the dictionary ` ` # Using comprehension list + keys () + lambda `   ` # initializing dictionary ` ` test_dict ` ` = ` ` {` ` 13 ` `: ` ` ’Hi’ ` `, ` ` 15 ` `: ` `’ Hello’ ` `, ` ` 16 : ’Gfg’ } ``   # initializing closest key search_key = 15.6   # printing the original dictionary print ( "The original dictionary is:" + str (test_dict))   # Using comprehension list + keys () + lambda # Closest key in the dictionary res = test_dict.get (search_key) or test_dict [ min (test_dict.keys (), key = lambda key: abs (key - search_key))]   # print result print ( " The value to the closest key: " + str ( res)) `

Output:

` The original dictionary is: {16: ’Gfg’, 13:’ Hi’, 15: ’Hello’} The value to the closest key: Gfg `

Method # 2: Using ` bisect_left () + OrderedDi ct () `
This method typically uses the binary search method to find the closest number. Being fast, it reverses the order and also returns 2 potential candidates for the closest values, the current and next key values ​​in the sequence. And it just returns the position of the key.

Output:

` The original dictionary is: {16: ’Gfg’, 13:’ Hi’, 15: ’Hello’} The position of closest key: 3 `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method

 ` # Python3 code to demonstrate how it works ` ` # Nearest key in dictionary ` ` # Using bisect_left () + OrderedDict () ` ` import ` ` collections ` ` import ` ` bisect `   ` # initializing dictionary ` ` test_dict ` ` = ` ` collections.OrderedDict () ` ` test_dict ` ` = ` ` {` ` 13 ` `: ` `’ Hi’ ` `, ` ` 15 < / code> : ’Hello’ ,  16 : ’ Gfg’ } ``   # closest key initialization search_key = 15.6    # printing the original dictionary print ( " The original dictionary is: " + str (test_dict))   # Using bisect_left () + OrderedDict () # Closest key in the dictionary res = bisect.bisect_left ( list (test_dict.keys ()), 15.6 )   # print result print ( " The position of closest key: " + str (res)) `