py::builtins::list::index¶
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¶
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¶
#include <pypp/builtins/list.hpp>
#include <pypp/builtins/print.hpp>
int main()
{
py::list<int> 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:
0
6