Python | Find the closest Key in dictionary Last Updated : 14 Mar, 2023 Comments Improve Suggest changes Like Article Like Report The search of keys in dictionary in python has been discussed many times. But sometimes, we may have a problem in which we require to fetch the key which is the nearest one of the given keys. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + keys() + lambda The combination of above functions can be used to perform the particular task of finding the closest key in the dictionary. The keys function can be used to access the keys from the dictionary, lambda function can be used to formulate the logic and list comprehension to apply that all to whole list. Python3 # Python3 code to demonstrate working of # Closest key in dictionary # Using list comprehension + keys() + lambda # initializing dictionary test_dict = {13 : 'Hi', 15 : 'Hello', 16 : 'Gfg'} # initializing nearest key search_key = 15.6 # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Using list comprehension + keys() + lambda # Closest key in dictionary res = test_dict.get(search_key) or test_dict[ min(test_dict.keys(), key = lambda key: abs(key-search_key))] # printing result print("The value to the closest key : " + str(res)) Time Complexity: O(n)Space Complexity: O(1) Output : The original dictionary is : {16: 'Gfg', 13: 'Hi', 15: 'Hello'} The value to the closest key : Gfg Method #2 : Using bisect_left() + OrderedDict() This method generally uses the binary search method of finding the nearest number. While being fast, it changes the ordering and also returns 2 potential candidates for nearest values, current and the next key's value in sequence. And just returns position of key. Python3 # Python3 code to demonstrate working of # Closest key in dictionary # Using bisect_left() + OrderedDict() import collections import bisect # initializing dictionary test_dict = collections.OrderedDict() test_dict = {13 : 'Hi', 15 : 'Hello', 16 : 'Gfg'} # initializing nearest key search_key = 15.6 # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Using bisect_left() + OrderedDict() # Closest key in dictionary res = bisect.bisect_left(list(test_dict.keys()), 15.6) # printing result print("The position of closest key : " + str(res)) Time Complexity: O(log n) Space Complexity: O(n) Output : The original dictionary is : {16: 'Gfg', 13: 'Hi', 15: 'Hello'} The position of closest key : 3 Comment More infoAdvertise with us Next Article Check if Tuple Exists as Dictionary Key - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Get the First Key in Dictionary - Python We are given a dictionary and our task is to find the first key in the dictionary. Since dictionaries in Python 3.7+ maintain insertion order, the first key is the one that was added first to the dictionary. For example, if we have the dictionary {'a': 10, 'b': 20, 'c': 30}, the first key is 'a'.Usi 2 min read Get the First Key in Dictionary - Python We are given a dictionary and our task is to find the first key in the dictionary. Since dictionaries in Python 3.7+ maintain insertion order, the first key is the one that was added first to the dictionary. For example, if we have the dictionary {'a': 10, 'b': 20, 'c': 30}, the first key is 'a'.Usi 2 min read Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp 2 min read Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp 2 min read Check if Tuple Exists as Dictionary Key - Python The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary.For example, given a dictionary d = {(3, 4): 'gfg', 3 min read Check if Tuple Exists as Dictionary Key - Python The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary.For example, given a dictionary d = {(3, 4): 'gfg', 3 min read Like