In this article, we will show you how to check if a nested key exists in a Dictionary in Python. To do that, you can use the has_key() function, the get() function, or the in operator. Let’s learn more about it with the explanation and examples below.
Check If A Nested Key Exists In A Dictionary In Python
Check if a nested key exists in a Dictionary with the in operator
To check if a nested key exists in a Dictionary, you can use the in operator combined with the if statement. If the key exists in a dictionary, the if statement will return True. Otherwise, it will return False.
Look at the example below.
def checkKeyExists(name, my_dict=dict()): # Check if a nested key exists in this dictionary. if name in my_dict.keys(): print(str(name) + ' exists in the dictionary') else: print(str(name) + ' does not exist in the dictionary') if __name__ == '__main__': # Create a dictionary author_info = { 'Thomas Valen': 2002, 'Toi Pham': 2002, 'Peter': 1985, 'Linda': 1990, 'Tiffy Doan': 1985, 'Son Goku': 1982 } # Print all keys in this dictionary. keys = author_info.keys() print('Keys: ' + ', '.join(keys)) # Try this function. name = 'Thomas Valen' checkKeyExists(name, author_info) # Check with another name. name = 'Ronaldo' checkKeyExists(name, author_info)
Output
Keys: Thomas Valen, Toi Pham, Peter, Linda, Tiffy Doan, Son Goku
Thomas Valen exists in the dictionary
Ronaldo does not exist in the dictionary
Check if a nested key exists in a Dictionary with the get() function
In this solution, we will use the get() function to check if a key exists in the given dictionary. If the get() function returns the default value, the dictionary does not contain this key.
Look at the example below.
def checkKeyExists(name, my_dict=dict()): # Get the value of the specified key in the dictionary. value = my_dict.get(name, -1) # Check if a nested key exists in this dictionary. if value != -1: print(str(name) + ' exists in the dictionary') else: print(str(name) + ' does not exist in the dictionary') if __name__ == '__main__': # Create a dictionary author_info = { 'Thomas Valen': 2002, 'Toi Pham': 2002, 'Peter': 1985, 'Linda': 1990, 'Tiffy Doan': 1985, 'Son Goku': 1982 } # Print all keys in this dictionary. keys = author_info.keys() print('Keys: ' + ', '.join(keys)) # Try this function. name = 'Thomas Valen' checkKeyExists(name, author_info) # Check with another name. name = 'Ronaldo' checkKeyExists(name, author_info)
Output
Keys: Thomas Valen, Toi Pham, Peter, Linda, Tiffy Doan, Son Goku
Thomas Valen exists in the dictionary
Ronaldo does not exist in the dictionary
Note that you must choose the default value of the get() function wisely.
Check if a nested key exists in a Dictionary with the has_key() function
For Python 2, you can use the has_key() function to check if a key exists in the given dictionary. For Python 3 or newer, you can not use this function because it was removed.
Look at the example below.
def checkKeyExists(name, my_dict=dict()): # Check if a nested key exists in this dictionary. if my_dict.has_key(name): print(str(name) + ' exists in the dictionary') else: print(str(name) + ' does not exist in the dictionary') if __name__ == '__main__': # Create a dictionary author_info = { 'Thomas Valen': 2002, 'Toi Pham': 2002, 'Peter': 1985, 'Linda': 1990, 'Tiffy Doan': 1985, 'Son Goku': 1982 } # Print all keys in this dictionary. keys = author_info.keys() print('Keys: ' + ', '.join(keys)) # Try this function. name = 'Thomas Valen' checkKeyExists(name, author_info) # Check with another name. name = 'Ronaldo' checkKeyExists(name, author_info)
Output
Keys: Thomas Valen, Toi Pham, Peter, Linda, Tiffy Doan, Son Goku
Thomas Valen exists in the dictionary
Ronaldo does not exist in the dictionary
Summary
We have shown you how to check if a nested key exists in a Dictionary in Python. To do that, you can use the in operator, the get() function, or the has_key() function. But note that the has_key() function is only used in Python 2, and you must choose the default value wisely if you use the get() function. So I recommend you use the in operator. It would be best. Thanks!

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R
Hello, this is not JavaScript
Thank you for reminding me!