Check if dictionary empty with == {}
Tim Chase
python.list at tim.thechases.com
Wed Aug 19 19:21:13 EDT 2015
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 odd to me, but maybe there is a valid use case, thus I
> decided to ask the community.
The only valid reason is "the person who wrote that line doesn't
speak idiomatic Python", and that it should be changed to "if not
mydict" at your next code checking :-D
-tkc
More information about the Python-list
mailing list