• Register
1 vote
50 views

Problem:

I am starting writing code in python. 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 "Present"
else:
  print "Thanks"

If there any other way to check this, please let me know.

15.3k points
reopened by

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

In your code, you have used the if and in method. This method simply uses if statement to check whether the given key exists in the dictionary. I don’t why are you thinking this is not the best way to check. This is one of the most used methods.

Well, as far as you don’t like to write code in this method. I can introduce you to another way to do the same thing by using the Inbuilt method keys()

def checkKey(dict, key): 
      
    if key in dict.keys(): 
        print("Present, ", end =" ") 
        print("value =", dict[key]) 
    else: 
        print("Thanks") 
  
# Driver Code 
dict = {'a': 100, 'b':200, 'c':300} 
  
key = 'b'
checkKey(dict, key) 
  
key = 'w'
checkKey(dict, key) 

The keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement and the ‘in’ operator to check if the key is present in the dictionary or not.

Thanks.

94.3k points
selected by

Related questions

0 votes
1 answer 18 views
18 views
Problem: I am in trouble Is anyone available to assist me and resolve this issue? Check if a key exists in dictionary python
asked May 18 Ifra 43.4k points
1 vote
1 answer 72 views
72 views
Problem : I was trying to check if key exists in dictionary python. So I wrote this code: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False ... unindent does not match any outer indentation level What is the problem in this program and how can I check if key exists in dictionary?
asked Apr 26, 2020 samhaz 5k points
0 votes
1 answer 35 views
35 views
Problem: I have a dictionary and I would like to get some values from it based on some keys. For example, I have a dictionary for users with their first name, last name, username, address, age and so on. Let's say, I only want to get one value (name) - either last ... if key exists, get value and stop checking. If not, move to next key. (3) username: if key exists, get value or return null/empty
asked Feb 21 Mashhoodch 13k points
0 votes
1 answer 7 views
7 views
Problem: Check if value exists in dictionary python.
asked Jul 12 Priscilla Gurpreet 74.7k points
0 votes
1 answer 9 views
9 views
Problem : Python check if key does not exist in dictionary.
asked Jul 2 Isac Christiaan 103k points
0 votes
2 answers 33 views
33 views
Problem: 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?
asked Feb 15 Mashhoodch 13k points
0 votes
1 answer 35 views
35 views
Problem Hi Kodlogs, How to check if a key is in a dictionary python? Please help Thanks
asked Oct 30, 2020 hashq 1.1k points
0 votes
1 answer 31 views
31 views
Problem: Can some body help me with this? Python check if key exists in json.
asked Mar 20 PkGuy 25.9k points
1 vote
1 answer 33 views
33 views
Problem: I wrote a python program to check whether a given key already exists in a dictionary. The dictionary I have is: (If exists, print Key is present and the value of the key. Otherwise print key is not present .) {'a': 233, 'b':588, 'c':967 ... checkKey() missing 1 required positional argument: 'key' Whats the problem with my program can any one help me to sort this out? Thanks in advance.
asked Apr 26, 2020 samhaz 5k points
0 votes
1 answer 117 views
117 views
Problem: Hello programmers! I am a newbie in the world of programming and I am working with dictionaries in C#. I want to check whether a key exists in Dictionary or not. If the key does not exist I want my program to automatically add the key to the dictionary. I have tried many ways but nothing works. I would be glad if any of you could help me. Thanks!
asked Nov 18, 2020 Code Learner 10.5k points