| Start |
|---|
| Getting the index for strings: str.index(), str.rindex() and str.find(), str.rfind()
The difference between All of these methods allow a start and end index: ValueError: substring not found |
| Searching for an element All built-in collections in Python implement a way to check element membership using ListTupleStringSetDict
|
| Getting the index list and tuples: list.index(), tuple.index()
ValueError: 15 is not in list But only returns the position of the first found element: |
| Searching key(s) for a value in dict
This could also be written as an equivalent list comprehension: If you only care about one found key: The first two functions will return a The other one will only return one key: and raise a StopIteration |
| Getting the index for sorted sequences: bisect.bisect_left() Sorted sequences allow the use of faster searching algorithms: ValueError For very large sorted sequences the speed gain can be quite high. In case for the first search approximatly 500 times as fast: While it's a bit slower if the element is one of the very first: |
| Searching nested sequences Searching in nested sequences like a The index of the outermost sequence if the value was found in the sequence: or the index of the outer and inner sequence: In general (not always) using |
| Searching in custom classes: contains and iter To allow the use of Suppose you have a class containing a Using membership testing is possible using even after deleting the Note: The looping RemarksAll searching algorithms on iterables containing |
Read Article
https://develop.horje.com/learn/1434/reference