In Python, we can use in to search in strings, lists, sets, etc. like
if x in string:
But which search algorithm is internally implemented?
Asked
Active
Viewed 60 times
1
CoolCoder
- 734
- 6
- 19
-
3Does this answer your question? [Python string 'in' operator implementation algorithm and time complexity](https://stackoverflow.com/questions/18139660/python-string-in-operator-implementation-algorithm-and-time-complexity) – python_user Apr 12 '21 at 01:55
-
1The duplicate only answers your question for strings, but the implementation is different for each type anyway. You might consider reading the source code for CPython for a specific implementation. – Selcuk Apr 12 '21 at 02:07
1 Answers
1
The implemented search algorithm is at the vendor's discretion. You need to consult your vendor's documentation for that. Also note that the algorithm will vary by data type: unrestricted sequences will require some form of linear search; sorted ones will have bisection or interpolation; dicts and sets use a constant-time hash key.
Prune
- 75,308
- 14
- 55
- 76