[Tutor] Testing Membership in a Sequence
Alan Gauld
alan.gauld at blueyonder.co.uk
Tue Mar 16 03:50:53 EST 2004
> What is the most pythonic way to test for membership of multiple
items
> in a sequence? For example:
>
> x = ['a', 'b', 'c']
>
> <pseudo code>
> is ('v' or 'b') in x
> </pseudo code>
I'd probably just do:
if ('v' in x) or ('b' in x):
...
For longer lists you could use some of the functional programming
constructs, but they can get quite convoluted. A simple comprehension
might be:
if [c for c in ['b', 'v'] if c in x]:
....
But that's just a loop in very thin disguise...
Alan G
More information about the Tutor
mailing list