17

I have the following code that reproduces a PyCharm warning,

Expression can be simplified

This expression detects equality comparison with a boolean literal.

seq_group = []
if seq_group == []: # warning here
   print("it is empty.")

if I change the code to,

if seq_group is None:

will fix the warning, but the real question is are None and []emplty list the same thing?

cheers

Community
  • 1
  • 1
daiyue
  • 7,196
  • 25
  • 82
  • 149

1 Answers1

14

are None and [] empty list the same thing?

Nope, and it will result in erroneous behavior:

seq_group = []

if seq_group is None:
    print("it is empty")

This doesn't print anything, None is completely different to [], value and identity wise. None indicates the absence of a value, [] indicates a list with no values. The confusion might arise from the fact that both happen to evaluate False in conditionals.

The warning is probably due to the fact that you can simply use seq_group with not instead of using a literal with ==:

if not seq_group:
    print("it is empty")
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • I also found that if `seq_group = None if not seq_group: print("it is none")`, `it is none` will be printed., which means `not` also works when some object is `None`. – daiyue Oct 05 '16 at 09:21