Dictionary bidirectional

bukzor workitharder at gmail.com
Mon Jul 14 00:16:38 EDT 2008


On Jul 13, 6:53 pm, Larry Bates <larry.ba... at websafe.com`> wrote:
> bearophileH... 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

/agree



More information about the Python-list mailing list