Python – Get next key in Dictionary

30 Dec, 2021
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the next key in order of dictionary. This can have application as from Python 3.6 onwards the dictionaries are ordered. Lets discuss certain ways in which this task can be performed. 
Method #1 : Using index() + loop (O (n))
The combination of above functions can be used to perform this task. In this, we perform the conversion of dictionary elements to list. And then index() is used to check for index and append the index count to get the next item.
Python3
# Python3 code to demonstrate working of# Get next key in Dictionary# Using index() + loop # initializing dictionarytest_dict = {'gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # initializing keytest_key = 'is' # Get next key in Dictionary# Using index() + looptemp = list(test_dict)try:    res = temp[temp.index(test_key) + 1]except (ValueError, IndexError):    res = None # printing resultprint("The next key is : " + str(res))
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best
 
 Method #2 : Using iter() + next() (O (n))
This is yet another way in which this task can be performed. In this, we convert the dictionary to iterator using iter() and then extract the next key using next(). 
Python3
# Python3 code to demonstrate working of# Get next key in Dictionary# Using iter() + next() # initializing dictionarytest_dict = {'gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # initializing keytest_key = 'is' # Get next key in Dictionary# Using iter() + next()res = Nonetemp = iter(test_dict)for key in temp:    if key == test_key:        res = next(temp, None) # printing resultprint("The next key is : " + str(res))
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best
 
Method #3 :  If you have lots of queries, they should be fast -> (O (1))
Create two additional dictionaries “index_of_key” and “key_of_index” in O (n). Then it is easy finding an index of any key of original dictionary and choose any other plus or minus this, check if it is in dictionary “key_of_index” and if, then get it.
Python3
#!/usr/bin/python3 # Python3 code to demonstrate working of# Get next key in Dictionary# Using two additional dictionaries# "index_of_key" and "key_of_index" # initializing dictionarytest_dict = {'gfg': 1, 'is': 2, 'best': 3} # prepare additional dictionarieski = dict()ik = dict()for i, k in enumerate(test_dict):    ki[k] = i   # dictionary index_of_key    ik[i] = k     # dictionary key_of_index # printing original dictionaryprint("The original dictionary is:", test_dict) # initializing key and offsettest_key = 'is'offset = 1  # (1 for next key, but can be any existing distance) # Get next key in Dictionaryindex_of_test_key = ki['is']index_of_next_key = index_of_test_key + offsetres = ik[index_of_next_key] if index_of_next_key in ik else None # printing resultprint("The next key is:", res)
Output : 
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The next key is : best
JosefHope
Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Introduction To PYTHON
Python OOPs Concepts
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers