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, it's

Re: Check if dictionary empty with == {}

2015-08-20 Thread Skip Montanaro
On Thu, Aug 20, 2015 at 12:44 PM, Steven D'Aprano mailto:st...@pearwood.info 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 empty. He

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 -s

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 seems odd to me, but maybe

Check if dictionary empty with == {}

2015-08-19 Thread Anton
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 seems odd to me, but maybe there is a valid use case,

Re: Check if dictionary empty with == {}

2015-08-19 Thread Skip Montanaro
On Wed, Aug 19, 2015 at 6:33 PM, MRAB pyt...@mrabarnett.plus.com 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

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

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 something which seems

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 --

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 == {}: do something