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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:22:01+00:00 2026-06-18T04:22:01+00:00

I am having some trouble with lists. So, basically, I have a list: a=[Britney

  • 0

I am having some trouble with lists. So, basically, I have a list:

a=["Britney spears", "red dog", "\xa2xe3"]

and I have another list, looking like:

b = ["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]

what I would like to do is check if elements in a are part of some element in b – and if so, remove them from b‘s element. So, I would like b to look like:

b = ["cat","dog","is stupid","good stuff","awesome"]

what is the most pythonic (in 2.7.x) way to achieve this?

I am assuming I can loop around to check each element, but I am not sure this is very efficient – I have a list (b) of around size 50k.

  • 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-18T04:22:03+00:00Added an answer on June 18, 2026 at 4:22 am

    I think I’d use a regex here:

    import re
    
    a=["Britney spears", "red dog", "\xa2xe3"]
    
    regex = re.compile('|'.join(re.escape(x) for x in a))
    
    b=["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]
    
    b = [regex.sub("",x) for x in b ]
    print (b)  #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ']
    

    This way, the regular expression engine can optimize the test for the list of alternatives.

    Here are a bunch of alternatives to show how different regexs behave.

    import re
    
    a = ["Britney spears", "red dog", "\xa2xe3"]
    b = ["cat","dog",
         "red dog is stupid", 
         "good stuff \xa2xe3", 
         "awesome Britney spears",
         "transferred dogcatcher"]
    
    #This version leaves whitespace and will match between words.
    regex = re.compile('|'.join(re.escape(x) for x in a))
    c = [regex.sub("",x) for x in b ]
    print (c) #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ', 'transfercatcher']
    
    #This version strips whitespace from either end
    # of the returned string
    regex = re.compile('|'.join(r'\s*{}\s*'.format(re.escape(x)) for x in a))
    c = [regex.sub("",x) for x in b ]
    print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transfercatcher']
    
    #This version will only match at word boundaries,
    # but you lose the match with \xa2xe3 since it isn't a word
    regex = re.compile('|'.join(r'\s*\b{}\b\s*'.format(re.escape(x)) for x in a))
    c = [regex.sub("",x) for x in b ]
    print (c) #['cat', 'dog', 'is stupid', 'good stuff \xa2xe3', 'awesome', 'transferred dogcatcher']
    
    
    #This version finally seems to get it right.  It matches whitespace (or the start
    # of the string) and then the "word" and then more whitespace (or the end of the 
    # string).  It then replaces that match with nothing -- i.e. it removes the match 
    # from the string.
    regex = re.compile('|'.join(r'(?:\s+|^)'+re.escape(x)+r'(?:\s+|$)' for x in a))
    c = [regex.sub("",x) for x in b ]
    print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transferred dogcatcher']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with my PHP code. Basically I have a list
I'm having some trouble figuring this out. I have an unordered list menu that
Im currently having trouble trying to understand linked lists. I have some code that
I'm having some trouble with a msbuild file. I'd like the build to have
I was having some trouble grokking the list comprehension syntax in Python, so I
I'm having troubles with creating a simple list (some expandable lists are already working).
I have some troubles with a method having a typed List parameter, inherited from
Having some trouble with R's garbage collection, when passing objects to C++. We have
Basically having some trouble with using Hover to hide or show an item. The
I'm having some trouble with structure. I have several objects of classes which extend

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.