How to check if all of the items below are included?

I found that there is a related question, how to find if at least one item exists in the list:
How to check if one of the following items is in the list?

But what is the best and pythonic way to find if all the items in the list exist?

Searching the docs I found this solution:

>>> l = ['a', 'b', 'c'] >>> set(['a', 'b']) <= set(l) True >>> set(['a', 'x']) <= set(l) False 

Another solution would be the following:

 >>> l = ['a', 'b', 'c'] >>> all(x in l for x in ['a', 'b']) True >>> all(x in l for x in ['a', 'x']) False 

But here you have to do more input.

Are there any other solutions?

+72
python list
Oct 14 '10 at 8:52
source share
7 answers

Operators like <= in Python are generally not overestimated to mean something other than "less than or equal to". This is unusual for a standard library - it smells like an old API.

Use the equivalent and more clearly named method, set.issubset . Note that you do not need to convert the argument to a set; he will do it for you if necessary.

 set(['a', 'b']).issubset(['a', 'b', 'c']) 
+81
Oct 14 '10 at 9:05
source share

I would probably use set as follows:

 set(l).issuperset(set(['a','b'])) 

or vice versa:

 set(['a','b']).issubset(set(l)) 

I find it more readable, but it can be killed. Kits are especially useful for calculating joins / intersections / differences between collections, but this might not be the best option in this situation ...

+55
Oct 14 2018-10-10T00:
source share

I like these two because they seem to be the most logical, the latter is shorter and probably the fastest (shown here using the set literal syntax that was ported to Python 2.7):

 all(x in {'a', 'b', 'c'} for x in ['a', 'b']) # or {'a', 'b'}.issubset({'a', 'b', 'c'}) 
+6
Oct. 14 '10 at 11:01
source share

What if your lists contain duplicates:

 v1 = ['s', 'h', 'e', 'e', 'p'] v2 = ['s', 's', 'h'] 

Sets do not contain duplicates. So the next line returns True.

 set(v2).issubset(v1) 

To count the number of duplicates, you can use the code:

 v1 = sorted(v1) v2 = sorted(v2) def is_subseq(v2, v1): """Check whether v2 is a subsequence of v1.""" it = iter(v1) return all(c in it for c in v2) 

So the next line returns False.

 is_subseq(v2, v1) 
+1
Aug 05 '17 at 7:44
source share

This is another method:

The code:

 def isAll(lis1, lis2): list = [] for i in lis2: for ii in lis1: if i == ii: list.append(True) else: list.append(False) if list.count(True) == len(lis1): return True else: return False lis1 = ["a","b","c"] lis2 = ["a","b"] isAll(lis1, lis2) #Output: False 
0
Jan 04 '19 at 16:21
source share

This was what I searched on the Internet, but unfortunately I didn’t find on the Internet, but while experimenting with the Python interpreter.

 >>> case = "caseCamel" >>> label = "Case Camel" >>> list = ["apple", "banana"] >>> >>> (case or label) in list False >>> list = ["apple", "caseCamel"] >>> (case or label) in list True >>> (case and label) in list False >>> list = ["case", "caseCamel", "Case Camel"] >>> (case and label) in list True >>> 

and if you have a long list of variables stored in the variable sublist variable

 >>> >>> list = ["case", "caseCamel", "Case Camel"] >>> label = "Case Camel" >>> case = "caseCamel" >>> >>> sublist = ["unique banana", "very unique banana"] >>> >>> # example for if any (at least one) item contained in superset (or statement) ... >>> next((True for item in sublist if next((True for x in list if x == item), False)), False) False >>> >>> sublist[0] = label >>> >>> next((True for item in sublist if next((True for x in list if x == item), False)), False) True >>> >>> # example for whether a subset (all items) contained in superset (and statement) ... >>> # a bit of demorgan law ... >>> next((False for item in sublist if item not in list), True) False >>> >>> sublist[1] = case >>> >>> next((False for item in sublist if item not in list), True) True >>> >>> next((True for item in sublist if next((True for x in list if x == item), False)), False) True >>> >>> 
0
Jan 29 '19 at 6:26
source share

An example of how to do this using a lambda expression:

 issublist = lambda x, y: 0 in [_ in x for _ in y] 
0
Jan 29 '19 at 15:49
source share



All Articles