[Tutor] consider a problem of finding if two lists have any item in common
Peter Otten
__peter__ at web.de
Sun Mar 21 13:39:55 EDT 2021
On 21/03/2021 16:01, Mats Wichmann wrote:
> On 3/21/21 8:53 AM, Peter Otten wrote:
>> On 21/03/2021 09:52, Alan Gauld via Tutor wrote:
>>> On 21/03/2021 07:15, Manprit Singh wrote:
>>>
>>>> lst1 = [2, 4, 6, 9]
>>>> lst2 = [2, 5, 9, 0]
>>>> this can be done using isdisjoint(other), it will return True if the
>>>> lists
>>>> have no items in common .The official documentation says nothing about
>>>> other , just need to know if other can be an iterable or it should
>>>> strictly
>>>> be a set or frozenset?
>>>> what should be the correct way ?
>>>
>>> Don't ask us, ask the interpreter, it knows best.
>>> And it gives faster answers too.
>>>
>>> If its answers aren't what you'd expect *then* ask us why?
>
> I'd perhaps also note that while the answer to the question asked may
> indeed be interesting, a lot of the time you actually want to know
> _what_ they have in common, and that would be a different operation.
> Which you can always truth-test to find out if they indeed had any
> common members.
The big difference between a.isdisjoint(b) and (not a & b) being that
the first can stop after the first common element. Though I wouldn't go
so far as to claim that the use cases are disjoint ;)
More information about the Tutor
mailing list