Check if dictionary empty with == {}

Skip Montanaro skip.montanaro at gmail.com
Thu Aug 20 14:40:31 EDT 2015


On Thu, Aug 20, 2015 at 12:44 PM, Steven D'Aprano
<mailto:steve at 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 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.

Skip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150820/14505412/attachment.html>


More information about the Python-list mailing list