.. index:: py::builtins::list::index .. _doxycast_pypp_classpy_1_1builtins_1_1list_1a15eece8d1b9d1122d921a2af783b4d15: .. _py-builtins-list-index: *py::builtins::list::*\ index ============================= .. code-block:: cpp std::size_t index(const_reference element) const; Returns the index of given element. ``py::builtins::list::index`` returns the index of given element. It first checks if there is any ``element`` with the same address as the address of given element, and return the index of that element if found, otherwise ``element`` compared by value with elements in the list. .. note:: When comparing by value, if the list contains more than one element with given value, index of the first one is returned. Parameter --------- ``element``: The element of look for. Return value ------------ The index of given element. Exceptions ---------- :ref:`py-builtins-ValueError` if the element can't be found. Complexity ---------- Linear in the position of ``element`` in the list if there is any element with the same address as the address of given ``element``, linear in the size of the list plus linear in the position of ``element`` otherwise. Example ------- .. code-block:: cpp #include #include int main() { py::list nums = {0, 2, 1, 4, 0, 3, 2}; py::print(nums.index(0)); // Argument is temporary reference, value 0 py::print(nums.index(nums[6])); // Argument is reference to element, value 2 } **Output**: .. code-block:: text 0 6