Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if listA== [ ] more simplified version [duplicate]

Tags:

When I type in the following code, PyCharm says "Expression can be further simplified". What is the more simplified version to this statement?

if listA == []:
  return "yes!"
  
like image 882
Eliza Avatar asked Jul 09 '16 13:07

Eliza


1 Answers

Empty lists evaluate as falsy, so you can also do this, which is what PyCharm may be talking about:

if not listA:
    return "yes!"

There are some side effects since the above code will return "yes!" whenever list is False, an empty string (""), None, an empty dict ({}), an empty set (set()) and basically anything else that python treats as falsy

like image 97
Eric Dill Avatar answered Sep 23 '22 13:09

Eric Dill