Expression can be simplified on list

Chris Angelico rosuav at gmail.com
Mon Sep 12 07:23:05 EDT 2016


On Mon, Sep 12, 2016 at 9:14 PM, Daiyue Weng <daiyueweng at gmail.com> wrote:
> Hi, I found that when I tried to make an equality test on empty like,
>
> if errors == []:
>
> PyCharm always warns me,
>
> Expression can be simplified.
>
> I am wondering what's wrong and how to fix this?
>

If you know that 'errors' is always going to be a list, you can check
for emptiness thus:

if not errors:

PyCharm recommends this syntax rather than the comparison with a
newly-created empty list. Your code has to actually construct and
dispose of a list, just for the comparison.

ChrisA



More information about the Python-list mailing list