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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:42:26+00:00 2026-06-03T21:42:26+00:00

I wrote this function to filter a list to show all items after a

  • 0

I wrote this function to filter a list to show all items after a given item is seen. Somewhat similar to the builtin string method str.rpartition(sep). I have a feeling there is a more compact way to do this, perhaps using a list comprehension. Any ideas?

def ignore_until(the_list, match):
    # Ignore all items in the_list prior to match
    found = False
    for index, item in enumerate(the_list):
        if item == match:
            found = True
            break
    if found:
        return the_list[index:]
    else:
        return []

my_list = ['red','orange','yellow','green']
assert ignore_until(my_list, 'yellow') == ['yellow','green']
assert ignore_until(my_list, 'blue') == []

EDIT:

After seeing the answers for the above question, I realized that 5 of the 6 answers focused on the index() builtin method for the list data type. Actually, I need to use a regular expression, and didn’t realize that omitting that from my question would affect people’s answers. Here’s the regex code:

import re
def ignore_until(the_list, pattern):
    # Ignore all items in the_list prior to the item containing pattern.
    found = False
    for index, item in enumerate(the_list):
        if re.search(string=item, pattern=pattern):
            found = True
            break
    if found:
        return the_list[index:]
    else:
        return []

my_list = ['red','orange','yellow','green']
assert ignore_until(my_list, 'yellow') == ['yellow','green']
assert ignore_until(my_list, 'blue') == []
  • 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-03T21:42:27+00:00Added an answer on June 3, 2026 at 9:42 pm

    Here’s a version that reproduces what str.partition does (i.e. returns three lists):

    def partition(lst, item):
        if item in lst:
            n = lst.index(item)
            return lst[:n], [item], lst[n+1:]
        else:
            return lst, [], []
    
    print partition(range(10), 7)
    

    and here’s a version which works with arbitrary iterables, not necessary lists:

    def partition(it, item):
        a = [[]]
        for x in it:
            if x == item and len(a) == 1:
                a.append([item])
                a.append([])
            else:
                a[-1].append(x)
        return a
    
    print partition((x for x in range(10)), 7)
    

    Improved version:

    def partition(it, item):
        a = []
        for x in it:
            if x == item:
                return a, [item], list(it)
            a.append(x)
        return a, [], []
    
    print partition((x for x in range(10)), 7)
    print partition((x for x in range(10)), 17)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function to get the unread count of google reader items. function
I wrote this little function to fill a drop down list with data from
this is somewhat of an odd question. I wrote a C function. Its 'like'
$(function(){ $('a').each(function(){ var x=this.href; this.href=www.somesitename.com/filter+this.href; }); }); i wrote the above jQuery script to
This drives me mad. I just can't understand it. I wrote a filter-function based
I wrote this function for filling closed loop, pixvali is declared globally to store
I wrote this function to get a pseudo random float between 0 .. 1
I wrote this function to communicate with an external program. Such program takes input
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
So I wrote the code below: jQuery('#contentWrapper').delegate(.addButton, click, function(){ addRow(jQuery(this)); }); function addRow(thisButton){ var

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.