On Apr 4, 11:20 am, "7stud" <[EMAIL PROTECTED]> wrote:
> On Apr 4, 10:55 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
>
> > <[EMAIL PROTECTED]> wrote in message
>
> >news:[EMAIL PROTECTED]
> > | For any list x, x.index(item) returns the index of the FIRST
> > | occurrence of the item in x. Is there a simple way to identify the
> > | LAST occurrence of an item in a list? My solution feels complex -
> > | reverse the list, look for the first occurence of the item in the
> > | reversed list, and then subtract its index from the length of the list
> > | - 1, i.e.
> > |
> > | LastOcc = len(x) - 1 - x[::-1].index(item)
> > |
> > | Is there a simpler solution?
>
> How about:
>
> l = [1, 2, 1, 3, 1, 5]
> target = 1
> for index, val in enumerate(l):
>     if val==1:
>         lastIndexOf = index
>
> print lastIndexOf

Nahh.  Horrible solution.  You should start at the end of the list.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to