How To Check if a Given Key Already Exists in a Dictionary

Two different ways to check

Jonathan Hsu
Oct 25, 2019 · 2 min read
Image for post
Image for post
Photo by Pisit Heng on Unsplash

A dictionary is a mutable — it has an unordered set of keys and values. Knowing how to check if a key exists is critical, both prior to assigning values, to avoid overwriting existing data, and when accessing values, to ensure the key exists.

Use the in keyword or the .get() method to inspect a dictionary for a given key.

The “in” Keyword

In Python, the in keyword queries an iterable for a specified value. In the event the iterable is a list, the values will be queried. For example:

Notice that the .keys() method is used to return the keys from our dictionary as a list of strings. We can speed this technique up because in may also be used directly with dictionaries. When querying a dictionary, the keys, and not the values, are searched.

The .get() Method

Referencing a key that does not exist in a dictionary will cause a KeyError exception, potentially breaking your code.

To avoid this scenario, you could use the above method to check for a key first. However, this can become very repetitive.

Alternatively, use .get() which will not cause an exception if the key does not exist. The method requires a single string (the key that will be retrieved) and accepts an optional second argument, which will be used as the default value when the key is not found. When a default value is not specified, None will be returned by .get() when the key is not found.

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Jonathan Hsu

Written by

Top writer with 1M+ views. Follow me at medium.com/code-85 and medium.com/growth-85

Better Programming

Advice for programmers.

Jonathan Hsu

Written by

Top writer with 1M+ views. Follow me at medium.com/code-85 and medium.com/growth-85

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store