0

I was doing some tutorials online and I being working with PyCharm, (an excellent IDE btw) but, it shows me that i could simplify a if statement

This is only for my knowledge, nothing more

if len(tasks) == []:

The suggestion says: Expression can be simplified. This inspection detects equality comparison with a boolean literal.

4 Answers4

3

len(tasks) == [] will always be False; you probably mean len(tasks) == 0. which is usually just used as

if not tasks:
    do_stuff()

the doc on truth value testing may be helpful.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • You are right that it is usually used as `if tasks:` but checking `if len(tasks) == 0:` is recommended for code clarity. – Adirio Jul 16 '19 at 06:36
  • 2
    PEP8 guide recommends using the falsy-ness of an empty list i.e. `if tasks:`. _For sequences, (strings, lists, tuples), use the fact that empty sequences are false._ https://www.python.org/dev/peps/pep-0008/#id51 – abdusco Jul 16 '19 at 06:52
  • @Adirio i disagre... to me `if tasks` looks more *pythonic* (not that this is a well defined term...). where does your recommendation come from? would you have a reference? – hiro protagonist Jul 16 '19 at 09:34
  • `if tasks:` is more pythonic and is recommended by the PEP8, as @abdusco said (https://www.python.org/dev/peps/pep-0008/#programming-recommendations, third starting from the bottom). What I meant is that writting `if len(tasks) == 0` is what I recommend to begginers as it is more clear. If you ask about my opinion, that recomendation of PEP8 goes against The Zen Of Python (explicit is better than implicit) (https://www.python.org/dev/peps/pep-0020/) – Adirio Jul 16 '19 at 09:47
1

You can just do

if tasks:
    # do something

which will evaluate to false if tasks list is empty [] or is None.


From PEP8 guide

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

https://www.python.org/dev/peps/pep-0008/#id51

abdusco
  • 9,700
  • 2
  • 27
  • 44
0

len() is the Built-in Functions in Python. This is what documentation explain about len() functions.

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

So len() function always return an integer value.

>>> _list = []
>>> len(_list)
0  # It's 0, because it's an empty list.
>>> len(_list) == []
False  # Yes, because 0 is not equal to list
>>> 0 == []
False  # Same as before, len(_list) always return 0

I think You are check the list is empty or not, can do it easily in this way.

if tasks:  # or len(tasks) != 0
    # do something when list has one or more values:
else:
    # do something when list is empty

You can refer more about from How do I check if a list is empty? question.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
0
if not tasks:
   do_stuff()

You can directly apply not operator on array to check if the array is empty or not.

Asnim P Ansari
  • 1,932
  • 1
  • 18
  • 41