Multiple ‘or’ condition in Python [duplicate]

Use not in and a sequence:

if fields[9] not in ('A', 'D', 'E', 'N', 'R'):

which tests against a tuple, which Python will conveniently and efficiently store as one constant. You could also use a set literal:

if fields[9] not in {'A', 'D', 'E', 'N', 'R'}:

but only more recent versions of Python (Python 3.2 and newer) will recognise this as an immutable constant. This is the fastest option for newer code.

Because this is one character, you could even use a string:

if fields[9] not in 'ADENR':

Leave a Comment