You can use the in
operator like this:
return x in ('A', 'B', 'C')
For Python 3.2+:
return x in {'A', 'B', 'C'}
From docs:
Python’s peephole optimizer now recognizes patterns such x in
{1, 2, 3}
as being a test for membership in a set of constants. The optimizer recasts theset
as afrozenset
and stores the pre-built constant.Now that the speed penalty is gone, it is practical to start writing membership tests using set-notation.