I have a python list which can have multiple strings and/or single string
my_list = ['string1', 'string2', 'string3', 'string4', 'string5']
#or,
my_list = ['string1']
#or,
my_list = ['string3']
#or,
my_list = ['string1', 'string3']
#or,
my_list = ['string4'] #... and so on
I want to search the list to be able to construct a condition which checks exactly for presence of 'string1' or 'string3' or 'string1 and string3'. I created an if condition by combining these list elements and an if statement.
search_string = ",".join(my_list)
if (search_string == "string1" or search_string == "string3" or search_string == "string1,string3"):
# Do my stuff
This solution works. But, I don't like my if statement structure. Is there a better way ? I did search through and try using "in" as in,
if search_string in ("string1" or search_string == "string3" or search_string == "string1,string3")
But, this didn't work. Is there a pythonic way of achieving this ? Looking for a single liner. The input "my_list" is made sure not to contain duplicate strings