Finding first and last index of some value in a list in Python

Joan Venge picture Joan Venge · Feb 6, 2009 · Viewed 145.7k times · Source

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)

Answer

SilentGhost picture SilentGhost · Feb 6, 2009

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)