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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:26:42+00:00 2026-05-15T02:26:42+00:00

I have a list of maybe a 100 or so elements that is actually

  • 0

I have a list of maybe a 100 or so elements that is actually an email with each line as an element. The list is slightly variable because lines that have a \n in them are put in a separate element so I can’t simply slice using fixed values. I essentially need a variable start and stop phrase (needs to be a partial search as well because one of my start phrases might actually be Total Cost: $13.43 so I would just use Total Cost:.) Same thing with the end phrase. I also do not wish to include the start/stop phrases in the returned list. In summary:

>>> email = ['apples','bananas','cats','dogs','elephants','fish','gee']
>>> start = 'ban'
>>> stop = 'ele'

# the magic here

>>> print new_email
['cats', 'dogs']

NOTES

  • While not perfect formatting of the email, it is fairly consistent so there is a slim chance a start/stop phrase will occur more than once.
  • There are also no blank elements.

SOLUTION

Just for funzies and thanks to everybody’s help here is my final code:

def get_elements_positions(stringList=list(), startPhrase=None, stopPhrase=None):
    elementPositionStart, elementPositionStop = 0, -1
    if startPhrase:
        elementPositionStart = next((i for i, j in enumerate(stringList) if j.startswith(startPhrase)), 0)
    if stopPhrase:
        elementPositionStop = next((i for i, j in enumerate(stringList) if j.startswith(stopPhrase)), -1)
    if elementPositionStart + 1 == elementPositionStop - 1:
        return elementPositionStart + 1
    else:
        return [elementPositionStart, elementPositionStop]

It returns a list with the starting and ending element position and defaults to 0 and -1 if the respective value cannot be found. (0 being the first element and -1 being the last).

SOLUTION-B

I made a small change, now if the list is describing a start and stop position resulting in just 1 element between it returns that elements position as an integer instead of a list which you still get for multi-line returns.

Thanks again!

  • 1 1 Answer
  • 1 View
  • 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-15T02:26:43+00:00Added an answer on May 15, 2026 at 2:26 am
    >>> email = ['apples','bananas','cats','dogs','elephants','fish','gee']
    >>> start, stop = 'ban', 'ele'
    >>> ind_s = next(i for i, j in enumerate(email) if j.startswith(start))
    >>> ind_e = next(i for i, j in enumerate(email) if j.startswith(stop) and i > ind_s)
    >>> email[ind_s+1:ind_e]
    ['cats', 'dogs']
    

    To satisfy conditions when element might not be in the list:

    >>> def get_ind(prefix, prev=-1):
        it = (i for i, j in enumerate(email) if i > prev and j.startswith(prefix))
        return next(it, None)
    
    
    >>> start = get_ind('ban')
    >>> start = -1 if start is None else start
    >>> stop = get_ind('ele', start)
    >>> email[start+1:stop]
    ['cats', 'dogs']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of approx. 500,000 strings, each approx. 100 characters long. Given
I have a list of paths (for lack of a better word, maybe bread
i have list of rows that user select and i want to delete them,
I have elements of a list in couchdb documents. Let's say these are 3
I have a website that lets people view rows in a table (each row
I have a list of approx. 10000 items. The current situation is that every
I have a list of objects, each of the same kind. Each object has
Say I have a 100 element numpy array. I perform some calculation on a
I have a list that must be filled with a bit of code behind
I have an unordered list, which has maybe 30 items. When one of these

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.