Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:30:38+00:00 2026-05-24T07:30:38+00:00

Say I have this list: li = [a, b, a, c, x, d, a,

  • 0

Say I have this list:

li = ["a", "b", "a", "c", "x", "d", "a", "6"]

As far as help showed me, there is not a builtin function that returns the last occurrence of a string (like the reverse of index). So basically, how can I find the last occurrence of "a" in the given list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:30:39+00:00Added an answer on May 24, 2026 at 7:30 am

    If you are actually using just single letters like shown in your example, then str.rindex would work handily. This raises a ValueError if there is no such item, the same error class as list.index would raise. Demo:

    >>> li = ["a", "b", "a", "c", "x", "d", "a", "6"]
    >>> ''.join(li).rindex('a')
    6
    

    For the more general case you could use list.index on the reversed list:

    >>> len(li) - 1 - li[::-1].index('a')
    6
    

    The slicing here creates a copy of the entire list. That’s fine for short lists, but for the case where li is very large, efficiency can be better with a lazy approach:

    def list_rindex(li, x):
        for i in reversed(range(len(li))):
            if li[i] == x:
                return i
        raise ValueError("{} is not in list".format(x))
    

    One-liner version:

    next(i for i in reversed(range(len(li))) if li[i] == 'a')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far i have this code: function changeGeoLoc($a,$b,$c){ echo <ul style='list-style-type: none; display:inline;'>; while(list(,$geoloc)
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
Let's say I have a list of words, something like this: ['The', 'Quick', 'Brown',
This is a long shot, I know... Let's say I have a collection List<MyClass>
Say I have a jquery function within JavaScript and I have this code so
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
Say i have this PHP code: $FooBar = a string; i then need a
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.