I'd like to find if a list of substrings is contained in a list. For example, I have:
string_list = ['item1', 'item2', 'subject3', 'subject4']
and list of substrings
substrings = ['item', 'subject']
I'd like to find if 'item' or 'subject' are included in any item of string_list. Individually, I would do something like:
any('item' in x for x in string_list)
This works for one substring but I would like to find a pretty way of searching for both strings in the list of substrings.
any(y in x for x in string_list for y in substrings)
Since the substrings are actually at the start, you can use str.startswith
which can take a tuple of prefixes:
any(x.startswith(('item', 'subject')) for x in string_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With