Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 2.7 presence in a dictionary

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.

http://img836.imageshack.us/img836/1274/screenshotpython.png

Thanks a lot for explaining :)

like image 740
jlanik Avatar asked Apr 24 '13 18:04

jlanik


People also ask

How do you check if a value is present in a dictionary in Python?

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.

How do you check the presence of a key 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.

How do you see if a key exists in a dictionary Python?

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.

How do you check if a word is in a dictionary Python?

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.

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

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:

How do I access the items of a dictionary in Python?

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.

What is an ordered dictionary in Python?

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.

What is the difference between *set and dictionary in Python?

*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.


1 Answers

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.)

like image 192
Edward Loper Avatar answered Oct 05 '22 23:10

Edward Loper