[Tutor] fine in interpreter, hangs in batch

Kent Johnson kent37 at tds.net
Mon Mar 19 19:03:03 CET 2007

Switanek, Nick wrote:
> Great, Kent, thanks. I thought that I had to check in the .keys() to see
> if the key was there. 
> It seems that the method you suggest will not work if I'm looking for a
> value in the dictionary. If that's correct, is there a fast alternative
> to searching through .values()

Not directly. If you only need to search by values (not keys) consider 
if you can reverse the keys and values. (This probably won't work if you 
can have many keys with the same value.)

Alternately, maintain a set of values in parallel with the dictionary. 
If there are a lot of places where you need to do this, maybe you want 
to make a dict subclass that manages a helper set. A good place to start 
might be this recipe:

It doesn't do what you want but it is similar and shows which dict 
methods must be overridden.

Hmm, deleting entries could be tricky, you don't know whether to delete 
from the helper. Maybe keep a dict that maps values to a count of how 
many times the value appears in the main dict.


More information about the Tutor mailing list