Is there any built-in methods that are part of lists that would give me the first and last index of some value, like:
verts.IndexOf(12.345) verts.LastIndexOf(12.345)
To find index of the first occurrence of an element in a given Python List, you can use index() method of List class with the element passed as argument. The index() method returns an integer that represents the index of first match of specified element in the List.
The first element is accessed by using blank value before the first colon and the last element is accessed by specifying the len() with -1 as the input.
The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised.
Sequences have a method index(value)
which returns index of first occurrence - in your case this would be verts.index(value)
.
You can run it on verts[::-1]
to find out the last index. Here, this would be len(verts) - 1 - verts[::-1].index(value)
If you are searching for the index of the last occurrence of myvalue
in mylist
:
len(mylist) - mylist[::-1].index(myvalue) - 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With