Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to check if key exists in dictionary Python

The Need to Check for a Key in a Dictionary

Think of Python dictionaries like a real-world dictionary. You open a dictionary to find the meaning of a word, the 'key', and get the 'value', which is the definition. Now, what if the word you're looking for is not there? This is why we need to check if a key exists in a dictionary in Python. If we try to access a key that does not exist, Python will throw an error.

First Method: Using the 'in' Keyword

Python has a straightforward way to achieve this using the 'in' keyword. The 'in' keyword in Python is used to check if a particular element exists in a certain collection of elements. Here, we will use it to check if a key exists in a dictionary.

Let's take an example. You have a dictionary that stores student names as keys and their grades as values.

grades = {
    'John': 'A',
    'Emma': 'B',
    'Kelly': 'A',
    'Jason': 'C'
}

Now, if you want to check if 'Kelly' is in the dictionary, you would do:

print('Kelly' in grades)  # Output: True

If you check for a student not in the dictionary, like 'Mike':

print('Mike' in grades)  # Output: False

Second Method: Using the get() Method

Another way to check if a key exists in a dictionary is by using the get() method. This method returns the value for a key if it exists in the dictionary. If not, it returns a default value that you can specify.

Let's use the same grades dictionary. If you want to get Kelly's grade:

print(grades.get('Kelly', 'No grade'))  # Output: A

If you try to get Mike's grade:

print(grades.get('Mike', 'No grade'))  # Output: No grade

The get() method is useful when you want to avoid errors when accessing keys that might not exist. Instead of stopping your program with an error, it will simply return the default value.

Third Method: Using the keys() Method

The keys() method in Python returns a view object that displays a list of all the keys in the dictionary. You can convert this view into a list and then check if the key exists in that list.

Here's how you can do it:

print('Kelly' in grades.keys())  # Output: True
print('Mike' in grades.keys())  # Output: False

This method is not as efficient as the previous ones, especially for large dictionaries, as it needs to create a new list and then perform the search. However, it can be useful in certain scenarios where you need to work with the keys outside of just checking for their existence.

Wrapping Up

In conclusion, Python provides you with several ways to check if a key exists in a dictionary. The method you choose will depend on your specific needs and circumstances.

Remember, dictionaries in Python are like real-world dictionaries. If you try to find a word that doesn't exist, you won't get a meaning. In Python, trying to access a key that doesn't exist in a dictionary will give you an error. Thankfully, Python has got you covered with several methods to check if a key exists in a dictionary!

The journey of learning programming is like a roller-coaster ride, with its ups and downs. There will be times when you will be stuck on a problem, unable to find a solution. But don't give up. The satisfaction and joy when you finally solve that problem are indescribable. So keep coding, keep exploring, and most importantly, have fun!