How to check if key exists in a python dictionary?
Published: 2021-05-28 11:33:48

In this python tutorial, we look at dictionaries and how to check if a key exists in a dictionary we also explain the code to facilitate further learning.

This tutorial is a part of our initiative at [Flexiple](https://flexiple.com/), to write short curated tutorials around often used or interesting concepts. However, in case you are here only for the solution use this [link](#code-to-check-if-a-key-exists-in-python-dictionary).   

### Table of Content

- [Why do we check if a key exists in python dictionary](#why-do-we-check-if-a-key-exists-in-python-dictionary)
- [Checking if a key exists using in operator](#checking-if-a-key-exists-using-in-operators)
- [Using the get() method](#using-the-get-method)
- [Closing thoughts](#closing-thoughts)

If you are here to learn how to check if a key exists in a dictionary it is most likely because of two reasons, either you are new to python or you tried using the `has_key` methods and were served with an error. In case it is the latter, this is because python3 has removed the `has_key` methods. However, there are other equally efficient methods and we look at them below.

## Why do we check if a key exists in python dictionary?

Dictionaries are common and extensively used data types in python. They hold key-value pairs and these values are accessed by the respective keys in order to perform many tasks on them. And this is why it is a good practice to check if the key exists below you try to access its relevant value. Doing so would also reduce the likelihood of facing errors. 

Now let's look at the different ways you can use to check if a key exists in python dictionary

## Checking if a key exists using `in` operators:

The `in` operator in python is a membership operator, it takes two parameters and checks if one is a member of the other and returns a boolean `True` or `False`. In our case, we the `in` operator to check if the key is a member of the dictionary. 

### Code to check if a key exists in python dictionary

```python
dict_1 = {"a": 1, "b":2, "c":3}
if "a" in dict_1:
    print("Exists")
else:
    print("Does not exist")

#Output = "Exists"
```

Now let's check for a negative case

```python
dict_1 = {"a": 1, "b":2, "c":3}
if "d" in dict_1:
    print("Exists")
else:
    print("Does not exist")

#Output = "Does not exist"
```

Similarly, the `not in` operator can also be used. However, remember the `in` operator is case sensitive hence you could either ensure all your keys are in the same case or you could use the `upper()` or `lower()` methods respectively. 

## Using the get() method

The `get()` method is a dict method that is used to return the value of the key passed as an argument and if the key is not present it returns either a default value (if passed) else it returns `None`. Using this method we can pass a key and check if a key exists in python dictionary.

### Syntax of get()

```python
dictionary.get(keyname, value)
```

Here `dictionary` is the name of the dict you indent to work with

### Parameters

`Keyname` - The keyname of the value to intent to return

`value` - Optional, this value is returned in case the key does not exist

### Code to check if the key exists in a dictionary using get()

```python
dict_1 = {"a": 1, "b":2, "c":3}
if dict_1.get("a") is not None:
    print("Exists")
else:
    print("Does not exist")

#Output = "Exists"
```

And for a negative case,

```python
dict_1 = {"a": 1, "b":2, "c":3}
if dict_1.get("d") is not None:
    print("Exists")
else:
    print("Does not exist")

#Output = "Does not exist"
```

While using this method keep in mind that this would not be accurate in case you have a key with the value `None` in case you don't then this method would work fine. 

## Closing thoughts

Although both these methods have their respective caveats to vary about they are far similarly and efficient in comparison to the other methods. And regarding other methods, I've come across methods that iterate over the dictionary and then check compare it to the key. However, all those methods so work they aren't efficient and should only be used to facilitate understanding of the concepts. But in case you are a learner, please feel free to try out such methods as well. 

Do leave your thoughts in the comment section below. Happy learning. :)