pythonsubstringtuples

Check if substring is in tuple of strings


Which is the most elegant way to check if there is an occurrence of several substrings in a tuple of strings?

string_tuple = ('first-answer', 'second-answer', 'third-answer') 
substr1 = 'first' 
substr2 = 'second' 
substr3 = 'third'

# PSEUDOCODE:
# if substr1 in string_tuple and \
#    substr2 in string_tuple and \
#    substr3 in string_tuple:
#     SHOULD return True

Solution

  • You need to iterate over the tuple for each of the substrings, so using any and all:

    all(any(substr in s for s in data) for substr in ['first', 'second', 'third'])