Does Python have a cleaner way to express "if x contains a|b|c|d..."?

PythonIf StatementPython 3.xSubstring

Python Problem Overview


The Pythonic way to check if a string x is a substring of y is:

if x in y:

Finding if x is equivalent to a, b, c, d, e, f or g is also Pythonic:

if x in [a,b,c,d,e,f,g]:

But checking if some string x contains either a, b, c, d, e, f or g seems clunky:

if a in x or b in x or c in x or d in x or e in x or f in x or g in x

Is there a more Pythonic method of checking if a string x contains an element of a list?

I know it is trivial to write this myself using a loop or using a regex:

re.search('(dog|cat|bird|mouse|elephant|pig|cow)', x)

but I was wondering if there was a cleaner way that does not involve regex.

Python Solutions


Solution 1 - Python

The Pythonic approach would be to use any():

if any(s in x for s in (a,b,c,d,e,f,g)):

From the linked documentation:

>any(iterable)

>Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: > def any(iterable): for element in iterable: if element: return True return False

Also, notice that I've used a tuple instead of a list here. If your a-g values are pre-defined, then a tuple would indeed be preferred. See: Are tuples more efficient than lists in Python?

Solution 2 - Python

if any(q in x for q in [a,b,c,d,e,f,g]):

I think that's about as short & Pythonic as you can get it.

Solution 3 - Python

A bit late to the party, but

not frozenset(x).isdisjoint(frozenset(y))

would work, and may be faster (algorithmically, but maybe not for smaller test cases).

Solution 4 - Python

without using any but simply max

def is_in(symbol, lst):
    return max([symbol in x for x in lst]) 

print is_in('a',['ae','br','tl'])
print is_in('c',['ae','br','tl'])

gives

>>> 
True
False

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontomView Question on Stackoverflow
Solution 1 - PythonarshajiiView Answer on Stackoverflow
Solution 2 - PythonjwodderView Answer on Stackoverflow
Solution 3 - PythonSnakes and CoffeeView Answer on Stackoverflow
Solution 4 - PythonkiriloffView Answer on Stackoverflow