I am trying to select certain strings in a list based on the existence of the words "foo" and "bar" or "baz" but I didn't manage to do it so far.
str_list = ['bar bar', 'foo foo', 'baz baz', 'bar foo', 'bar baz', 'foo baz']
for i in str_list:
if "foo" and ("bar" or "baz") in i:
print(i)
For the code above, I expected the output to be:
bar foo
foo baz
However, the output is:
bar bar
bar foo
bar baz
I tried this and it didn't work also:
if "foo" in i and ("bar" or "baz") in i:
print(i)
=======================================================
Solution:
for i in str_list:
if "foo" in i and any(x in i for x in ["bar", "baz"]):
print(i)
Output:
bar foo
foo baz