22

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!"
  
Eliza
  • 379
  • 1
  • 8
  • 3
    Check out http://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty – petabyte Jul 09 '16 at 13:16
  • If anyone finds it useful, I created a youtube tutorial comparing the different ways to check if list is empty https://www.youtube.com/watch?v=8V88bl3tuBQ. This is discussed in the PEP as well https://www.python.org/dev/peps/pep-0008/#programming-recommendations – Brendan Metcalfe Jun 27 '20 at 23:08

1 Answers1

12

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

Eric Dill
  • 2,166
  • 2
  • 17
  • 15