Re: Check if dictionary empty with == {}

2015-08-20 Thread Skip Montanaro
On Thu, Aug 20, 2015 at 12:44 PM, Steven D'Aprano wrote: > Testing for "any Falsey value" and "an empty dict" are not the same, > naturally they will perform differently. Sure, but the OP's original note explicitly said he had a dict and asked how to test if it was emp

Re: Check if dictionary empty with == {}

2015-08-20 Thread Steven D'Aprano
On Thu, 20 Aug 2015 11:54 pm, Skip Montanaro wrote: > On Wed, Aug 19, 2015 at 10:16 PM, Steven D'Aprano < > steve+comp.lang.pyt...@pearwood.info> wrote: > >> So maybe it's a micro-optimization? > > > Note, however, that the original post compared "mydict == {}" with "not > mydict". In that case

Re: Check if dictionary empty with == {}

2015-08-20 Thread Laurent Pointal
Anton wrote: > Probably a silly question. > Let's say I have a dictionary mydict and I need to test if a dictionary is > empty. > > I would use > > if not mydict: > """do something""" > > But I just came across a line of code like: > > if mydict == {}: > """do something""" > > which s

Re: Check if dictionary empty with == {}

2015-08-20 Thread Skip Montanaro
On Wed, Aug 19, 2015 at 10:16 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > So maybe it's a micro-optimization? Note, however, that the original post compared "mydict == {}" with "not mydict". In that case, it's decidedly not an optimization: firefly% python2.7 -m timeit

Re: Check if dictionary empty with == {}

2015-08-19 Thread Steven D'Aprano
On Thursday 20 August 2015 08:57, Anton wrote: > Probably a silly question. > Let's say I have a dictionary mydict and I need to test if a dictionary is > empty. > > I would use > > if not mydict: > """do something""" > > But I just came across a line of code like: > > if mydict == {}: >

Re: Check if dictionary empty with == {}

2015-08-19 Thread Skip Montanaro
On Wed, Aug 19, 2015 at 6:33 PM, MRAB wrote: > Well, that depends on the intention. > > Is it checking whether the dictionary is empty, or whether it's an > empty dictionary (and not, say, an empty list)? > Sure, that's a possibility. I would argue that "mydict == {}" is also not the idiomatic w

Re: Check if dictionary empty with == {}

2015-08-19 Thread Tim Chase
On 2015-08-19 15:57, Anton wrote: > Probably a silly question. > Let's say I have a dictionary mydict and I need to test if a > dictionary is empty. > > I would use > > if not mydict: > """do something""" > > But I just came across a line of code like: > > if mydict == {}: > """do some

Re: Check if dictionary empty with == {}

2015-08-19 Thread MRAB
On 2015-08-20 00:15, Skip Montanaro wrote: Comparison against {} will be less efficient. You need to create a dictionary every time instead of just checking the length of your dictionary, which is likely stored in the header. So, it will work, but certainly isn't idiomatic Python. Well, that de

Re: Check if dictionary empty with == {}

2015-08-19 Thread Skip Montanaro
Comparison against {} will be less efficient. You need to create a dictionary every time instead of just checking the length of your dictionary, which is likely stored in the header. So, it will work, but certainly isn't idiomatic Python. Skip -- https://mail.python.org/mailman/listinfo/python-li