• Register
1 vote
26 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.

12 7 7
15,250 points

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.

13 9 6
94,240 points

Related questions

1 vote
1 answer 41 views
41 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 samhaz 5k points
3 votes
1 answer 12 views
12 views
Problem: Hi all, I am trying to figure out the dictionary related problems in python. Dictionary is really fun to learn and work with. Let’s say, I have a dictionary dict = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} If ... needed to check every value inside the dictionary what can I do for that? So, I want to know how to check if a value is in a dictionary python?
asked Mar 24 Gavin 15.3k points
0 votes
1 answer 28 views
28 views
I wrote a code to check if a dictonary is empty or not. I wrote this code in python : 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: socket.send("No ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) Does my isEmpty returns true and is there any problem in my code.
asked Mar 11 LizzyM 6.1k points
1 vote
1 answer 21 views
21 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3 LizzyM 6.1k points
1 vote
1 answer 11 views
11 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 samhaz 5k points