-1

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

abs
  • 101
  • 1
  • 7
  • Edited the question to make it clear. The list "my_list" can have multiple combination of strings as I have shown in the input. But, I want to be able to find condition when there's only "string1" or "string3" or "string1 and string3" – abs May 15 '16 at 21:49
  • The list "my_list" can be multiple combinations of the said strings. Idea is to be able to detect the condition when there's only "string1" or "string3" or "string1,string3". Remaining combination of list inputs would have to return false – abs May 15 '16 at 21:54
  • Sorry if I wasn't clear. As I said above input list "my_list" can have multiple combination of strings. – abs May 15 '16 at 21:58
  • What happens for `my_list = ['string1', 'string3',"string1"]`? – Padraic Cunningham May 15 '16 at 22:13
  • "my_list" is made sure not to have duplicate values – abs May 15 '16 at 22:15
  • Ok, just wanted to make sure as sets will count dupes as one so using `st.issuperset(['string1', 'string3',"string1"])` would return True so uniqueness is important if `['string1', 'string3',"string1"]` would not be considered a match. – Padraic Cunningham May 15 '16 at 22:16

2 Answers2

2

You want to check for a superset:

st = {"string3","string1"}

if st.issuperset(my_list):

Demo:

In [1]: st = {"string3", "string1"}

In [2]: my_list = ['string1', 'string2', 'string3']

In [3]: st.issuperset(my_list)
Out[3]: False

In [4]: my_list = ['string1', 'string3']

In [5]: st.issuperset(my_list)
Out[5]: True

In [6]: my_list = ['string1']

In [7]: st.issuperset(my_list)
Out[7]: True

In [8]: my_list = ['string3']

In [9]: st.issuperset(my_list)
Out[9]: True
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Cool solution, Thanks ... Didn't know about issuperset , marked this as the answer – abs May 15 '16 at 22:17
  • No worries, using set.superset avoids creating multiple sets. – Padraic Cunningham May 15 '16 at 22:19
  • 1
    @abs This is functionally equivalent to my answer, just more efficient if your search list contains more than a few elements. Obviously you misunderstood what I had posted and didn't even bother to correct yourself when I asked for qualification. – Tom Karzes May 15 '16 at 22:19
  • I still don't understand why I was down voted for this question .. If it's because of not being clear with the question, I have corrected it – abs May 15 '16 at 22:20
  • 1
    @abs, it was probably a hard question to word unless you knew you were actually looking for superset, maybe edit the question and/or title adding that information will help get the dvs offset. Often people who dv don't come back to the question but when others see a clear description they will upvote the question. – Padraic Cunningham May 15 '16 at 22:25
  • @TomKarzes : Sorry bro ... There were multiple comments and I was not able to keep up-to-date .. If it's of any consolation, I just realized that you provided similar solution and I was a bit dumb to understand it :( – abs May 15 '16 at 22:26
  • @abs Both solutions have their place. For very small numbers of search strings, my solution would be faster. For larger numbers of search strings, this solution is faster. It's a question of when the overhead of creating a set becomes justified. Your post suggested you never had more than 2 search strings, in which case my solution would be faster. But I deleted it since you erroneously claimed you wanted differerent behavior. – Tom Karzes May 15 '16 at 22:38
  • 1
    @TomKarzesm you should undelete your answer to show an alternative way, set.superset will be very fast thought even for small my_lists – Padraic Cunningham May 15 '16 at 22:49
1

You can use sets:

>>> test = {'string1', 'string3'}
>>> my_list = {'string1', 'string2', 'string3', 'string4', 'string5'}
>>> len(my_list) > 0 and my_list <= test
False
>>> my_list = {'string1', 'string3'}
>>> len(my_list) > 0 and my_list <= test
True
>>> my_list = {'string3'}
>>> len(my_list) > 0 and my_list <= test
True

The <= condition checks if my_list is a subset of {'string1', 'string3}

Schore
  • 885
  • 6
  • 16
  • There is no need to convert my_list to a set, making a set of the strings to search for and using set.issuperset does exactly the same thing without creating another set unnecessarily – Padraic Cunningham May 15 '16 at 22:12