Dictionary bidirectional

Larry Bates larry.bates at websafe.com`
Sun Jul 13 21:53:44 EDT 2008


bearophileHUGS at lycos.com wrote:
> bukzor:
>> You need to use two dictionaries. Here's a class that someone's
>> written that wraps it up into a single dict-like object for you:
>> http://www.faqts.com/knowledge_base/view.phtml/aid/4376
> 
> It contains code like:
> 
> try:
>     del self.data[item]
> except KeyError:
>     pass
> 
> Exceptions are useful in python, but with dictionaries this is
> probably faster (and shorter), even if it may perform two lookups:
> 
> if item in self.data:
>     del self.data[item]
> 
> Bye,
> bearophile

The only case where it would be faster would be if most of the keys were NOT in 
the dictionary (rather odd use case).  Otherwise I believe you will find the 
first way quicker as the exceptions are infrequent.

-Larry



More information about the Python-list mailing list