I need to verify if a list is a subset of another - a boolean return is all I seek.
Is testing equality on the smaller list after an intersection the fastest way to do this?
Performance is of utmost importance given the amount of datasets that need to be compared.
Adding further facts based on discussions:
What would be the optimum solution given the scenario?
The performant function Python provides for this is set.issubset. It does have a few restrictions that make it unclear if it's the answer to your question, however.
A list may contain items multiple times and has a specific order. A set does not. To achieve high performance sets work on hashable objects only.
Are you asking about subset or subsequence (which means you'll want a string search algorithm)? Will either of the lists be the same for many tests? What are the datatypes contained in the list? And for that matter, does it need to be a list?
Your other post intersect a dict and list made the types clearer and did get a recommendation to use dictionary key views for their set-like funcitonality. In that case it was known to work because dictionary keys behave like a set (so much so that before we had sets in Python we used dictionaries). One wonders how the issue got less specific in three hours.
>>> a = [1, 3, 5] >>> b = [1, 3, 5, 8] >>> c = [3, 5, 9] >>> set(a) <= set(b) True >>> set(c) <= set(b) False >>> a = ['yes', 'no', 'hmm'] >>> b = ['yes', 'no', 'hmm', 'well'] >>> c = ['sorry', 'no', 'hmm'] >>> >>> set(a) <= set(b) True >>> set(c) <= set(b) False