Python Glossary Русский

all Python 3operatorfunction

operator.indexOf

sequences

The index of the first occurrence of b in a.

Syntax

operator.indexOf(a, b)

Unlike list.index it accepts any iterable, including a generator, but partly consumes it: everything up to and including the match has been read. If the item is absent it raises ValueError; for "find or default" next((i for i, x in enumerate(xs) if x == v), -1) is handier.

Examples

import operator
print(operator.indexOf([1, 2, 3], 3))   # → 2
print(operator.indexOf('hello', 'l'))  # → 2
print(operator.indexOf((10, 20, 30), 20))  # → 1
print(operator.indexOf(iter([5, 6, 7]), 7))  # → 2
print(operator.indexOf([1, 2, 3], 9))  # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy