0

I need something that checks the length of a list. Maybe something like this, but this does not work:

if len(list) != 1 or 2:
   # Do something

if len(list) == 1 or 2:
   # Do something different

Ok i figured this out myself:

if len(list) == 1:
   # Do something
elif len(list) == 2:
   # Do the same something

if len(list) != 2:
   # Do something different
elif len(list) != 1:
   # Do something different
ivan_pozdeev
  • 31,209
  • 16
  • 94
  • 140

1 Answers1

3

Something like

if 1 <= len(list) <= 2:
    ...

or:

if len(list) in (1, 2):
    ...

should work

nalzok
  • 13,395
  • 18
  • 64
  • 118
Francisco
  • 10,005
  • 5
  • 34
  • 42