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 6563641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:52:28+00:00 2026-05-25T13:52:28+00:00

Using Python, I’m trying to read a list or strings backwards. When finding the

  • 0

Using Python, I’m trying to read a list or strings backwards. When finding the item of interest, I want to print all of those items from that point to the end of the list. I can do this without recursion and it works fine, but I feel like there’s a nicer way to do this with recursion. 🙂

Example without recursion:

items = ['item1', 'item2', 'item3', 'item4', 'item5']

items_of_interest = []
items.reverse()
for item in items:
    items_of_interest.append(item)
    if item == 'item3':
        break
    else:
        continue

items_of_interest.reverse()
print items_of_interest
['item3', 'item4', 'item5']

Update:

To add clarity to the question, the list is actually the output of a grep of a set of strings from a log file. The set of strings may be repeating and I only want the last set.

  • 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-25T13:52:28+00:00Added an answer on May 25, 2026 at 1:52 pm

    Recursion wouldn’t make this simpler, it would make it more complicated.

    for i, item in enumerate(reversed(items), 1):
        if item == 'item3':
            items_of_interest = items[-i:]
            break
    else:
        # 'item3' wasn't found
    

    seems to be the simplest efficient way to do this to me. You only have to iterate over the list from the end to 'item3', since reversed returns an iterator.

    Edit: if you don’t mind iterating over the whole list to create a reversed version, you can use:

    i = list(reversed(items)).index('item3')
    items_of_interest = items[-i-1:]
    

    which is even simpler. It raises an error if 'item3' isn’t in the list. I’m using list(reversed()) instead of [:] then reverse() because it’s one iteration over the list instead of two.

    Edit 2: Based on your comment to the other answer, my first version does what you want — searches for the item from the end without iterating over the whole list. The version in the question has to iterate the list to reverse it, as does my second version.

    A minimally modified, but more efficient, version of your original would be:

    items_of_interest = []
    for item in reversed(items):
        items_of_interest.append(item)
        if item == 'item3':
            break
    items_of_interest.reverse()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python I want to replace all URLs in a body of text with
Using Python 2.4, how do I print a list in a nice tabular format?
Using Python, I'm trying to convert a sentence of words into a flat list
Using Python I want to be able to draw text at different angles using
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip
Using Python 2.6, is there a way to check if all the items of
Using Python's Imaging Library I want to create a PNG file. I would like
Using Python how do you reduce a list of lists by an ordered subset
Using Python, I want to know whether Java is installed.
Using Python 2.6 I want to be able to convert numbers such as 00000,

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.