How to check if a key exists in a dictionary in Python

In this tutorial, we will learn, How to check if a key exists in a dictionary in Python.
As we know that in a dictionary we have “keys” as well as “Values” separated by a semicolon (:).
There are various methods to check if a particular key does exist or not.
Let’s find out the number of methods to find keys from a random dictionary.

a={4:'abc','acb':'dfg','gfs':54}

Check if a key exists in a dictionary in Python

Methods:-

  • To check a particular key using get function.
    print(a.get(4))
print(a.get(4))

If the key (4)exists in the dictionary then it will return its value.
If the key does not exist then it will return “None”

print("values-",a.get(4))
abc

Let’s try out some other keys which do not exist in the dictionary.

print(a.get(5))
None
  • We can check keys by using the condition.
    if a.get(5):
    print(“values-“,a.get(4))
    else:
    print(“No key found”)
if a.get(4):
    print("values-",a.get(4))
else:
    print("No key found")

We can check keys existence and print accordingly.

If 4 exists in the dictionary then it will return its value otherwise it will show an error.

if a.get(4):
 print("values-",a.get(4))
else:
 print("No key found")

values- abc

If 4 do not exist in python then it will print “No key found”

if a.get(5):
    print("values-",a.get(4))
else:
    print("No key found")

No key found
  • We can also get the list of all keys using various methods.
    print(dict.keys(a))
    print(a.keys())
    print(list(a.keys()))
print(dict.keys(a))
print(a.keys())
print(list(a.keys()))
dict_keys([4, 'acb', 'gfs'])
dict_keys([4, 'acb', 'gfs'])
[4, 'acb', 'gfs']
>>>
  • To check keys using the loop function
    for x in dict.keys(a):
    print(x)
for x in dict.keys(a):
    print(x)

If key exists in a dictionary then it will return all
dictionary otherwise it will show an error.

4
acb
gfs
  •  It will give the result in the form of True or False
    f=4 in dict(a)
    print(f)

 

f=4 in dict(a)
print(f)

It will print as :

True

Let’s try out another key that does not exist in the dictionary.

f=5 in dict(a)
print(f)

Since 5 does not exist in the dictionary then it will print as:

False
  •  We can also check key existence using a function.
    def key(b):
    if b in dict(a):
    print(“Key available, its values-“,a.get(b))
    else:
    print(“No key found”)
def key(b):
    if b in dict(a):
        print("Key available, its values-",a.get(b))
    else:
        print("No key found")

Using the function method we can check any key randomly.

key("gfs")

If “gfs” exist in python it will give result as Key available, its values- 54. Otherwise, it will print “No key found”.

Since “gfs” exist in the dictionary then it will print as :

Key available, its values- 54

Let’s try with another key that does not exist in the dictionary.

key("dsc")

Since “dsc” does not exist in the dictionary, then it will print as:

No key found

You may also read:

2 responses to “How to check if a key exists in a dictionary in Python”

  1. Shubham says:

    Helpful content

  2. pushkar Jaiswal says:

    good for beginners

Leave a Reply

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