Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW: the `x in foo` test calls the `foo.__contains__` magic method.

For a list, the only way to implement it is by iterate through it (see the `list_contains` function in the CPython code).

But for the special `range` object, it can implement the `__contains__` efficiently by looking at the start/stop/step (see the `range_contains` source code).

Although the Hack 1 is for demonstration purpose, in most cases you can just do `999999 in range(1000000)`.

In my test, the same `999999 in foo` is 59.1ns for the range object, 27.7ns for the set, 6.7ms for the list. The set is the fastest, except converting the range object to the set takes 21ms.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: