I want to test a presence of a key in a dictionary as 'if key is not in dictionary: do something' I have already done this already multiple times, but this time it behaves strangely.
particularly:
termCircuit = termCircuitMap[term]
returns KeyError
when I debugged this code in Eclipse PyDev, i got the following (using expressions):
term in termCircutiMap # prints False
term in termCircuitMap.keys() # prints True
Do anyone understand how this is possible? I thought that if something is 'in' the key set then it is 'in' the dictionary.
I'm attaching a screenshot of the evaluation.
Thanks a lot for explaining :)
Check if a value exists in a dictionary: in operator, values() To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use not in to check if a value does not exist in a dictionary.
Using has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not.
Check If Key Exists Using has_key() The has_key() method is a built-in method in Python that returns true if the dict contains the given key, and returns false if it isn't.
To simply check if a key exists in a Python dictionary you can use the in operator to search through the dictionary keys like this: pets = {'cats': 1, 'dogs': 2, 'fish': 3} if 'dogs' in pets: print('Dogs found!') # Dogs found! A dictionary can be a convenient data structure for counting the occurrence of items.
Use Python to Check if a Key Exists: Python keys Method Python dictionary come with a built-in method that allows us to generate a list-like object that contains all the keys in a dictionary. Conveniently, this is named the.keys () method. Printing out dict.keys () looks like this:
You can access the items of a dictionary by referring to its key name, inside square brackets: The keys () method will return a list of all the keys in the dictionary. The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates. As of Python version 3.7, dictionaries are ordered.
*Set items are unchangeable, but you can remove and/or add items whenever you like. **As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
You might see this behavior if your key's __hash__
function is not properly defined. E.g., the following gives roughly the same behavior as you describe:
import random
class Evil(int):
def __hash__(self):
return random.randint(0, 10000)
evil_vals = [Evil(n) for n in range(10)]
dict_with_evil_keys = dict((evil_val, None)
for evil_val in evil_vals)
print evil_vals[0] in dict_with_evil_keys # prints False
print evil_vals[0] in dict_with_evil_keys.keys() # prints True
In this case, I'm generating random hash values, which is obviously a bad idea. A less obvious problem that would have the same effect might be if your key values are mutable. (Generally, mutable values should never define __hash__
, and should not be useable as keys in dictionaries.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With