Check if dictionary empty with == {}

Skip Montanaro skip.montanaro at
Thu Aug 20 20:40:31 CEST 2015

On Thu, Aug 20, 2015 at 12:44 PM, Steven D'Aprano
<mailto:steve at> 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 was used to seeing/using "not
mydict", but had recently encountered "mydict == {}".  Given those
preconditions, the correct answer is that "not mydict" is the way to
go (idiomatic Python). Using "mydict == {}" is clearly suboptimal.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list