Indentifying the LAST occurrence of an item in a list

Terry Reedy tjreedy at udel.edu
Wed Apr 4 13:34:52 EDT 2007


"7stud" <bbxx789_05ss at yahoo.com> wrote in message 
news:1175707250.349825.223470 at p77g2000hsh.googlegroups.com...
| How about:
|
| l = [1, 2, 1, 3, 1, 5]
| target = 1
| for index, val in enumerate(l):
|    if val==1:
|        lastIndexOf = index
|
| print lastIndexOf

You might want to initialize lastIndexOf (to None, for instance).
If len(l) = 1000000, you might prefer to start from the end, as I 
suggested.

tjr






More information about the Python-list mailing list