[Solved] IF statement (checking for a string in a list) behave weirdly [duplicate]


if 'ddd' or 'eee' in test

is evaluated as:

if ('ddd') or ('eee' in test):

as a non-empty string is always True, so or operations short-circuits and returns True.

>>> bool('ddd')
True

To solve this you can use either:

if 'ddd' in test or 'eee' in test:

or any:

if any(x in test for x in ('ddd', 'eee')):

solved IF statement (checking for a string in a list) behave weirdly [duplicate]