Check if a given key already exists in a dictionary

0 votes

I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code:

if 'key1' in dict.keys():
  print "blah"
else:
  print "boo"

I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?

Nov 26, 2020 in Python by Rajiv
• 8,910 points
678 views

1 answer to this question.

0 votes

in is the intended way to test for the existence of a key in a dict.

d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("this will execute")

if "nonexistent key" in d:
    print("this will not")

If you wanted a default, you can always use dict.get():

d = dict()

for i in range(100):
    key = i % 10
    d[key] = d.get(key, 0) + 1

and if you wanted to always ensure a default value for any key you can either use dict.setdefault() repeatedly or defaultdict from the collections module, like so:

from collections import defaultdict

d = defaultdict(int)

for i in range(100):
    d[i % 10] += 1

but in general, the in keyword is the best way to do it.

answered Nov 26, 2020 by Gitika
• 65,910 points

Related Questions In Python

0 votes
1 answer

How to check if a given string matches a particular pattern in Python?

You can use re module of python ...READ MORE

answered Jul 3, 2019 in Python by Neel
• 3,020 points
983 views
0 votes
1 answer

How to check if a value exists in pandas dataframe index?

Hello @kartik, Basically instead of raising exception I ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
6,159 views
0 votes
2 answers

Obtaining a value when given a key in python dicionaries

Yes you can check below code dictionary = ...READ MORE

answered Nov 25, 2021 in Python by Suhas
591 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 58,090 points
22,692 views
0 votes
1 answer

How to write a code In Python where input will be 1 then the output will be 2?

You can go through this:  def num(number): ...READ MORE

answered Oct 7, 2020 in Python by Gitika
• 65,910 points
923 views
0 votes
1 answer

What will be the output of below code and why? x=[1,2,3,4,5] print(x.insert(2,3))

If you write x.insert(2,3) and then print x ...READ MORE

answered Oct 14, 2020 in Python by Gitika
• 65,910 points
3,695 views
0 votes
1 answer

How to print only the line which starts with P?

Hi, you might have found another way ...READ MORE

answered Dec 2, 2020 in Python by Tyler
• 140 points
848 views
0 votes
1 answer

How do you use a variable in a regular expression?

Instead of using the /regex/g syntax, you can construct ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,910 points
319 views
0 votes
2 answers

How do I check if a variable exists in python?

Python doesn’t have a specific function to ...READ MORE

answered Dec 28, 2020 in Python by Carlos
11,913 views
0 votes
1 answer

How do I check if a given Python string is a substring of another one?

Try using in like this: >>> x = 'hello' >>> y ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,910 points
358 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP