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

  • SEARCH
  • Home
  • 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 8720273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:00:00+00:00 2026-06-13T07:00:00+00:00

Possible Duplicate: Finding first and last index of some value in a list in

  • 0

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

Hi I was wondering if someone could help me with Python. I am trying to create a code that returns the last index of the last occurrence of an item in a list in a recursive way. So in a list [1,2,3,4,5,2] the last it should return 4. It only takes in 2 variables which are the list and the item that it is searching for. If it does not find any matching variable then it returns -1.

So far I have this:

def lstIndex(lst, item):
    if len(lst) == 0:
        return -1
    place = lst[0]
    if place == item:
        print(place)
        #just return the index
        return lst.index(place)
    else:
        return lstIndex(lst[1:],item)
  • 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-06-13T07:00:02+00:00Added an answer on June 13, 2026 at 7:00 am

    I’m not quite 100% sure I know what you want. Your statement that “… in a list of [1,2,3,4,5,2] the last it should return 4 …” has me a bit confused; I think you want to return the index of the last appearance of your specified item. So, for 4 to be the result in the list specified, item must be 5.

    As noted elsewhere, a recursive function would not be the most efficient or Pythonic solution here. I’d prefer a solution like the first one in nneonneo’s answer.

    However, if it must be recursive, I believe the code below gets what you want. Instead of stepping through the list from the front (by using [1:]), you need to step backwards by using [:-1] as the index range when passing the list in the recursive call:

    def lstIndex(lst, item):
        if len(lst) == 0:
            return -1
        elif lst[-1] == item:
            return len(lst) - 1
        else:
            return lstIndex(lst[0:-1], item)
    

    I tested with the following:

    the_list = [1,2,3,4,5,2]
    print lstIndex(the_list, 2)
    print lstIndex(the_list, 1)
    print lstIndex(the_list, 3)
    print lstIndex(the_list, 4)
    print lstIndex(the_list, 5)
    print lstIndex(the_list, 6)
    print lstIndex(the_list, 0)
    

    With the following output:

    5
    0
    2
    3
    4
    -1
    -1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: finding index of an item closest to the value in a list
Possible Duplicate: Detecting closest or parent div I need some help about finding elements
Possible Duplicate: Finding the index of a list in a loop Assume you have
Possible Duplicates: Finding duplicate files and removing them. In Python, is there a concise
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm finding that dictionary
Possible Duplicate: Sorting or Finding Max Value by the second element in a nested
Possible Duplicate: Multiprocessing launching too many instances of Python VM I'm trying to use
Possible Duplicate: Peak-finding algorithm for Python/SciPy I'm looking to find local maxima in a
Possible Duplicate: Finding an enum value by its Description Attribute I have a generic
Possible Duplicate: Finding a single number in a list Given an array of numbers,

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.