Oliver Dixon
Oliver Dixon

Reputation: 13

Checking the elements of a list for multiple strings

Say I have a list:

['[name]\n', 'first_name,jane\n', 'middle_name,anna\n', 'last_name,doe\n', '[age]\n', 'age,30\n', 'dob,1/1/1988\n']

How could I check if the strings 'jane', 'anna' and 'doe' are ALL contained in an element of the list.

Upvotes: 1

Views: 41

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117876

For each name you can use any to see if it is contained in any of the strings in the list, then make sure this is true for all of the names

>>> data = ['[name]\n', 'first_name,jane\n', 'middle_name,anna\n', 'last_name,doe\n', '[age]\n', 'age,30\n', 'dob,1/1/1988\n']
>>> names = ['jane', 'anna', 'doe']
>>> all(any(name in sub for sub in data) for name in names)
True

Upvotes: 3

Related Questions