Re: Way for see if dict has a key

2006-06-30 Thread Georg Brandl
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Re: Way for see if dict has a key

2006-06-30 Thread Eric Deveaud
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 ? >

Re: Way for see if dict has a key

2006-06-30 Thread Alex Martelli
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Re: Way for see if dict has a key

2006-06-30 Thread Paul McGuire
"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

RE: Way for see if dict has a key

2006-06-30 Thread Cihal Josef
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

Re: Way for see if dict has a key

2006-06-30 Thread Georg Brandl
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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: >

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
"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

Re: Way for see if dict has a key

2006-06-30 Thread looping
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:

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Re: Way for see if dict has a key

2006-06-30 Thread Michele Petrazzo
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

Re: Way for see if dict has a key

2006-06-30 Thread André
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

Re: Way for see if dict has a key

2006-06-30 Thread Michele Petrazzo
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

Re: Way for see if dict has a key

2006-06-30 Thread Michele Petrazzo
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

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Re: Way for see if dict has a key

2006-06-30 Thread Bruno Desthuilliers
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

Re: Way for see if dict has a key

2006-06-30 Thread Michele Petrazzo
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

Re: Way for see if dict has a key

2006-06-30 Thread Chance Ginger
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

Re: Way for see if dict has a key

2006-06-30 Thread Fredrik Lundh
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

Way for see if dict has a key

2006-06-30 Thread Michele Petrazzo
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