mercredi 5 juin 2019

Two not x.startswith() in if statement

Let's suppose I have this list

y = ['bat','bats','cat','cats','dog','dogs']

I want to remove everything that starts with bat or cat. Therefore, only 'dog' and 'dogs' should be left in the list.

For some reason, if I type this:

for i in y:
     if not i.startswith('bat') or not i.startswith('cat'):
          print(i)

Each item will be printed.

But, if I just have one of the clauses:

for i in y:
     if not i.startswith('bat'):
          print(i)

'bat' and 'bats' are not printed as expected.

What is going on with the second not i.startswith() expression?

Thank you all in advance!

Aucun commentaire:

Enregistrer un commentaire