Re: Way for see if dict has a key
- From: Bruno Desthuilliers <onurb@xxxxxxxxxxx>
- Date: Fri, 30 Jun 2006 14:55:15 +0200
Fredrik Lundh wrote:
"André" wrote:
Perhaps Bruno meant this:
try:
... my_dict[key] ...
except:
...
when we expect that the key will most often be in my_dict so that
exception will be raised rarely, otherwise use
on my machine, "key in dict" is about twice as fast as the full try/getitem con-
struct when the key is present in the dict,
Doesn't it depends on the number of keys in the dict ?
so that's not a very good optimization.
FWIW, I personaly didn't meant to present it as an optimisation - just
as one more possible way to test the existence of a given key...
now, if the OP had been interested in the associated value, things might have
been a bit different.
</F>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xxxxxxxxxxx'.split('@')])"
.
- Follow-Ups:
- Re: Way for see if dict has a key
- From: Fredrik Lundh
- Re: Way for see if dict has a key
- References:
- Way for see if dict has a key
- From: Michele Petrazzo
- Re: Way for see if dict has a key
- From: Bruno Desthuilliers
- Re: Way for see if dict has a key
- From: Michele Petrazzo
- Re: Way for see if dict has a key
- From: André
- Re: Way for see if dict has a key
- From: Fredrik Lundh
- Way for see if dict has a key
- Prev by Date: Re: list comprehension
- Next by Date: Re: delete first line in a file
- Previous by thread: Re: Way for see if dict has a key
- Next by thread: Re: Way for see if dict has a key
- Index(es):
Relevant Pages
|