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
  • 33,874
  • 19
  • 107
  • 152

1 Answers1

3

Something like

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

or:

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

should work

nalzok
  • 14,965
  • 21
  • 72
  • 139
Francisco
  • 10,918
  • 6
  • 34
  • 45