Check if Key exists in Dictionary | Python

Python Dictionaries are just unordered collection of key:value pairs where key must be immutable and value can be anything. But if given a dictionary let’s say name a_dict then How to tell whether it have a key or not? 🤔

If your not aware of What are Python Dictionaries? Then see this article first – Dictionaries in Python Programming Langauge.

Moreover it’s quite crucial before accessing a key’s value from dictionary to check even if dictionary have that key. Otherwise it would raise errors. Personally I prefer checking whether key is there in dictionary or not becoming accessing value of that key, I would recommend you to do the same.
For checking whether a key exists in a Python Dictionary or not you can use any of three methods – in operator, keys() method, get() method.

Using Python in operator for checking whether key exists in dictionary or not?

This is one of simplest approach for checking existence of a key inside dictionary or not. But problem with this way is that you need to know beforehand what’s key name which you want to look for inside Dictionary.

Syntax for this Testing Method
keyname in dictname

Example

x = { 1: "Apple", 2: "Banana"}
1 in x
# Returns true as 1 is a key in x dictionary

4 in x
# Returns false as 4 is not a key in x dictionary

Checking whether key is in Dictionary or not using keys() method

For checking whether a key is there inside a dictionary keys() method can also be used. Python keys() method return a dict_keys object which is an iterable. That’s why you cannot directly access its elements rather you need to convert dict_keys object to some other type of object whose elements can be easily accessed. Converting dict_keys object to Python list is best option here.
Below is a code example show How to use keys() method for checking whether a key is there in a dictionary or not?

Example

x = { 1:"Apple", 2:"Banana" }
y = x.keys()
z = list(y)        # z is a list of keys of dictionary x [1,2]

keyname = 3           # Select what keyname you want to search for

for i in z:
	if i == keyname:
		print(keyname + " is a key in dictionary x")
	else:
		print(keyname + " is not a key in dictionary x")

Using Python’s get() method for checking if Key exists in Dictionary or not?

Python’s get() method which is usually used for accessing value of a key from dictionary, can also be used for checking whether some key is there or not in Dictionary, its so because get() method returns value of key if key exists but return none when key doesn’t exist.

  • get() method returns value of key, if key exists in Dictionary
  • get() returns none if key doesn’t exist in Dictionary

Below are some examples showing How get() method can be used for checking whether some key is there in Dictionary or not?

Example

x = { 1:"Apple", 2:"Banana" }
keyname = 3
if x.get(keyname):
	print("Dictionary x have key " + keyname)
else:
	print("Dictionary x doesn't have key " + keyname)

Conclusion

In this article, I’ve described how to check if a key exists in a dictionary or not. For doing this I’ve listed three approaches – Using in operator, keys() method and get() method. You can use any one of this depending upon specifics of your code.
But if your not sure which one should I use and your stuck somewhere in code. Then please come down in comments with specific code and I’ll help you out. 😄

Moreover, Dictionaries are one of five built-in Python Data Types other being – Numbers, Sequence, Boolean and Sets. If you want to know about other types as well see – Five built-in Python Data Types article.

Happy Coding 🥳

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts