Bruno Desthuilliers wrote:
>>> value = dict.get(key, None)
>>
>>
>> value = dict.get(key)
>
> Yes - but :
> 1/ not everybody knows that dict.get() takes a second optional param.
> Note that, while it happens that the default return value of dict.get()
> is the same as in the above example, but
Fredrik Lundh wrote:
> Bruno Desthuilliers wrote:
>
>> Seems that if "key in dict" do a simple linear search
>
>
> that would be rather silly.
>
> hint: http://pyref.infogami.com/__contains__
Of course. It's just me being silly...
:(
Thanks F
--
bruno desthuilliers
python -c "print '@'.joi
Bruno Desthuilliers wrote:
> Seems that if "key in dict" do a simple linear search
that would be rather silly.
hint: http://pyref.infogami.com/__contains__
--
http://mail.python.org/mailman/listinfo/python-list
Bruno Desthuilliers wrote:
> Fredrik Lundh wrote:
> > Bruno Desthuilliers wrote:
> >
> >>> on my machine, "key in dict" is about twice as fast as the full
> >
> >>> try/getitem construct when the key is present in the dict,
> >
> >>
> >> Doesn't it depends on the number of keys in the dict ?
>
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Michele Petrazzo wrote:
>
> > what the preferred way for see if the dict has a key?
> > We have a lot of solutions:
> >
> > key in dict
>
> new syntax (2.3 and later).
Fine with Python 2.2 as well, actually -- so, "new" only in an
_extremely_ loose sen
Georg Brandl wrote:
> Bruno Desthuilliers wrote:
>
>> looping wrote:
>>
>>> Michele Petrazzo wrote:
>>>
Bruno Desthuilliers wrote:
>> but what the better
>
>
> Depends on the context.
>
If know only one context: see if the key are into the dict... What
Fredrik Lundh wrote:
> Bruno Desthuilliers wrote:
>
>>> on my machine, "key in dict" is about twice as fast as the full
>
>>> try/getitem construct when the key is present in the dict,
>
>>
>> Doesn't it depends on the number of keys in the dict ?
>
>
> why would it depend on the number of key
Paul McGuire wrote:
> Another factor to consider is if "default" is not something simple like 0 or
> None, but is an object constructed and initialized with some expensive
> initialization code (like access to a database). In this case:
>
> dict.get(key, ExpensiveObjectToCreate())
>
> alwa
Bruno Desthuilliers wrote:
>> on my machine, "key in dict" is about twice as fast as the full
>> try/getitem construct when the key is present in the dict,
>
> Doesn't it depends on the number of keys in the dict ?
why would it depend on the number of keys in the dict ?
--
http://mail.pytho
"Michele Petrazzo" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> > when we expect that the key will most often be in my_dict so that
> > exception will be raised rarely
>
> I didn't thought this because if I think that a key aren't in a dict, I
> use dict.get(key, default)
>
Anothe
If dict.has_key('key'):
print dict['']
Josef Cihal
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Georg Brandl
Sent: Friday, June 30, 2006 5:07 PM
To: python-list@python.org
Subject: Re: Way for see if dict has a key
Bruno
Bruno Desthuilliers wrote:
> looping wrote:
>> Michele Petrazzo wrote:
>>
>>>Bruno Desthuilliers wrote:
>>>
>but what the better
Depends on the context.
>>>
>>>If know only one context: see if the key are into the dict... What other
>>>context do you know?
>>>
>>
>> Why do you
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
looping wrote:
> Michele Petrazzo wrote:
>
>>Bruno Desthuilliers wrote:
>>
but what the better
>>>
>>>Depends on the context.
>>>
>>
>>If know only one context: see if the key are into the dict... What other
>>context do you know?
>>
>
> Why do you want to do that ?
>
> if key in dict:
>
"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-
stru
Michele Petrazzo wrote:
> Bruno Desthuilliers wrote:
> >> but what the better
> >
> > Depends on the context.
> >
>
> If know only one context: see if the key are into the dict... What other
> context do you know?
>
> Michele
Why do you want to do that ?
if key in dict:
value = dict[key]
else:
Michele Petrazzo wrote:
>> Michele Petrazzo wrote:
>>
> key in dict
new syntax (2.3 and later).
>>> So, following it, it can be used for the operations like len?
>>
>> what's "it" in this sentence?
>
> It, is the thought that the new 2.3 introduce.
support for "key in dictionary" form wa
André wrote:
> Michele Petrazzo wrote:
>> Bruno Desthuilliers wrote:
but what the better
>>> Depends on the context.
>>>
>> If know only one context: see if the key are into the dict... What other
>> context do you know?
>>
>> Michele
>
> Perhaps Bruno meant this:
>
> try:
>... my_dict[k
Michele Petrazzo wrote:
> Bruno Desthuilliers wrote:
> >> but what the better
> >
> > Depends on the context.
> >
>
> If know only one context: see if the key are into the dict... What other
> context do you know?
>
> Michele
Perhaps Bruno meant this:
try:
... my_dict[key] ...
except:
...
wh
Fredrik Lundh wrote:
> Michele Petrazzo wrote:
>
key in dict
>>> new syntax (2.3 and later).
>> So, following it, it can be used for the operations like len?
>
> what's "it" in this sentence?
>
It, is the thought that the new 2.3 introduce.
Michele
--
http://mail.python.org/mailman/listi
Bruno Desthuilliers wrote:
>> but what the better
>
> Depends on the context.
>
If know only one context: see if the key are into the dict... What other
context do you know?
Michele
--
http://mail.python.org/mailman/listinfo/python-list
Michele Petrazzo wrote:
> >> key in dict
> >
> > new syntax (2.3 and later).
>
> So, following it, it can be used for the operations like len?
what's "it" in this sentence?
> len(dict) -> len(dict.keys()) ?
len(dict.keys()) is a lousy way to spell len(dict).
--
http://mail.python.org/mai
Michele Petrazzo wrote:
> Hi ng,
> what the preferred way for see if the dict has a key?
> We have a lot of solutions:
>
> key in dict
> key in dict.keys()
> dict.has_key(key)
> ...
try:
dict[key]
except KeyError:
...
else:
...
> but what the better
Depends on the context.
--
bruno dest
Fredrik Lundh wrote:
> Michele Petrazzo wrote:
>
>> what the preferred way for see if the dict has a key?
>> We have a lot of solutions:
>>
>> key in dict
>
> new syntax (2.3 and later).
>
So, following it, it can be used for the operations like len?
len(dict) -> len(dict.keys()) ?
Thanks,
Mi
On Fri, 30 Jun 2006 10:19:46 +, Michele Petrazzo wrote:
> Hi ng,
> what the preferred way for see if the dict has a key?
> We have a lot of solutions:
>
> key in dict
> key in dict.keys()
> dict.has_key(key)
> ...
>
> but what the better or the more "pythonic"?
>
> Thanks,
> Michele
It is
Michele Petrazzo wrote:
> what the preferred way for see if the dict has a key?
> We have a lot of solutions:
>
> key in dict
new syntax (2.3 and later).
> key in dict.keys()
inefficient and pointless.
> dict.has_key(key)
old syntax; use for code that needs to be backwards compatible.
> but
Hi ng,
what the preferred way for see if the dict has a key?
We have a lot of solutions:
key in dict
key in dict.keys()
dict.has_key(key)
...
but what the better or the more "pythonic"?
Thanks,
Michele
--
http://mail.python.org/mailman/listinfo/python-list
27 matches
Mail list logo