0

I have a list and It is easy to find if an element is present in a list or not but how to find if a combination of two or all the elements are present in a list?

listi = ["head","strong","pain","chores"]

for one element:

if i in listi:
    print("available")
else:
    print("Not available")

But How to check combination of elements like if head,strong is present in a list or pain,cores,strong or any conbimation of elements is present in a list? How to do?

I understand that we can do via subsets but in that case, I have to create subset of every combination which is not possible

  • ``if "head" in listi and "strong" in listi:``? Do you care about duplicates, e.g. is `"head","head"` present in the list if there is only one `"head"`? – MisterMiyagi Jul 22 '21 at 10:45
  • An easier one: ```if "head" and "strong" in listi: print("Available")``` –  Jul 22 '21 at 10:46
  • @SanskarSingh And a wrong one. Try ``if "head" and "strong" in ["strong"]: print(True)``. – MisterMiyagi Jul 22 '21 at 10:47
  • This is also answered https://stackoverflow.com/questions/16579085/how-can-i-verify-if-one-list-is-a-subset-of-another – Main Jul 22 '21 at 10:49
  • No it is answered partially. but in my case, I have to create a subset fof every combination which is not possible – Enigmaderockz Jul 22 '21 at 10:59
  • "I have to create a subset fof every combination which is not possible" Why not? How do you know which combinations to check for if you cannot create them? – MisterMiyagi Jul 22 '21 at 11:06
  • "I have to create a subset of every combination" -- every combination *of what*? Is there another "main" list which has all the possible values? – Andrew Jaffe Jul 22 '21 at 11:13

4 Answers4

3

You might use set arithmetic for this task as follows:

listi = ["head","strong","pain","chores"]
required = set(["pain","chores","strong"])
print(required.issubset(listi))

output

True

If you want know more about, read Sets in docs

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Thanks but In this approach then I have to create a required variable for every combination which is not possible like for pain.,chores it is one variable and then chores,head another one. not possible – Enigmaderockz Jul 22 '21 at 10:54
  • @Enigmaderockz How do you know which combinations to check for if you cannot create them? Note that there is no need to store them into variables, e.g. ``set(["pain","chores","strong"]).issubset(listi)`` works (as do many other variations). – MisterMiyagi Jul 22 '21 at 11:07
  • FWIW, ``set(["pain","chores","strong"])`` should just be ``{"pain","chores","strong"}``. – MisterMiyagi Jul 22 '21 at 11:11
2

You can use all and a comprehension:

listi = ["head","strong","pain","chores"]
try1 = ["head", "strong"]
try2 = ["head", "face"]

all(e in listi for e in try1)  ## True
all(e in listi for e in try2)  ## False
Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
1

You can use the any / all methods:

if all([word_to_check in listi for word_to_check in words])
Dr. Prof. Patrick
  • 1,280
  • 2
  • 15
  • 27
  • 1
    Heads up that the list created by the ``[`` ``]`` is not needed. Omitting the ``[`` ``]`` turns this into a memory efficient generator. – MisterMiyagi Jul 22 '21 at 10:46
1

You can use sets:

list_ = ["head","strong","pain","chores"]
sublist = ["head", "strong"]

intersect = set(list_) & set(sublist)

if intersect:
   ...
  • Thanks but In this approach then I have to create a sublist for every combination which is not possible like for pain.,chores it is one variable and then chores,head another one. not possible – Enigmaderockz Jul 22 '21 at 10:55
  • it's not necessary you can have a list of all sublists, and then you for statement. for example sublists = [["head", "strong"], [strong","pain","chores"]] list_as_set = set(list_) all_has_intersect = all(list_as_set & set(sublist) for sublist in sublists) – Roksolanka Fedkovych Jul 22 '21 at 10:59