Why searching in a set is much faster than in a list ?

Chris Angelico rosuav at gmail.com
Wed Sep 28 05:07:35 EDT 2016


On Wed, Sep 28, 2016 at 6:46 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Lawrence D’Oliveiro <lawrencedo99 at gmail.com>:
>
>> On Wednesday, September 28, 2016 at 6:51:17 PM UTC+13, ast wrote:
>>> I noticed that searching in a set is faster than searching in a list.
>>
>> That’s why we have sets.
>
> I would have thought the point of sets is to have set semantics, just
> like the point of lists is to have list semantics.

And set semantics are what, exactly? Membership is a primary one.
"Searching in a set" in the OP's language is a demonstration of the
'in' operator, a membership/containment check.

(Other equally important set semantics include intersection and union,
but membership inclusion checks are definitely up there among primary
purposes of sets.)

ChrisA



More information about the Python-list mailing list