John Alba
John Alba

Reputation: 315

python: 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?

tuple = ('first-second', 'second-third', 'third-first') 
substr1 = 'first' 
substr2 = 'second' 
substr3 = 'third'
#if substr1 in tuple and substr2 in tuple and substr3 in tuple:
#    should return True

Upvotes: 4

Views: 6258

Answers (2)

AChampion
AChampion

Reputation: 30268

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'])

Upvotes: 3

s16h
s16h

Reputation: 4855

any(substr in str_ for str_ in tuple_)

You can start with that and look at all() as well.

Upvotes: 5

Related Questions