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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:53:58+00:00 2026-05-17T17:53:58+00:00

# I have 3 lists: L1 = [1, 2, 3, 4, 5, 6, 7,

  • 0
# I have 3 lists:
L1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
L2 = [4, 7, 8]
L3 = [5, 2, 9]
# I want to create another that is L1 minus L2's memebers and L3's memebers, so:
L4 = (L1 - L2) - L3  # Of course this isn't going to work

I’m wondering, what is the “correct” way to do this. I can do it many different ways, but Python’s style guide says there should be only 1 correct way of doing each thing. I’ve never known what this was.

  • 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-17T17:53:59+00:00Added an answer on May 17, 2026 at 5:53 pm

    Here are some tries:

    L4 = [ n for n in L1 if (n not in L2) and (n not in L3) ]  # parens for clarity
    
    tmpset = set( L2 + L3 )
    L4 = [ n for n in L1 if n not in tmpset ]
    

    Now that I have had a moment to think, I realize that the L2 + L3 thing creates a temporary list that immediately gets thrown away. So an even better way is:

    tmpset = set(L2)
    tmpset.update(L3)
    L4 = [ n for n in L1 if n not in tmpset ]
    

    Update: I see some extravagant claims being thrown around about performance, and I want to assert that my solution was already as fast as possible. Creating intermediate results, whether they be intermediate lists or intermediate iterators that then have to be called into repeatedly, will be slower, always, than simply giving L2 and L3 for the set to iterate over directly like I have done here.

    $ python -m timeit \
      -s 'L1=range(300);L2=range(30,70,2);L3=range(120,220,2)' \
      'ts = set(L2); ts.update(L3); L4 = [ n for n in L1 if n not in ts ]'
    10000 loops, best of 3: 39.7 usec per loop
    

    All other alternatives (that I can think of) will necessarily be slower than this. Doing the loops ourselves, for example, rather than letting the set() constructor do them, adds expense:

    $ python -m timeit \
      -s 'L1=range(300);L2=range(30,70,2);L3=range(120,220,2)' \
      'unwanted = frozenset(item for lst in (L2, L3) for item in lst); L4 = [ n for n in L1 if n not in unwanted ]'
    10000 loops, best of 3: 46.4 usec per loop
    

    Using iterators, will all of the state-saving and callbacks they involve, will obviously be even more expensive:

    $ python -m timeit \
      -s 'L1=range(300);L2=range(30,70,2);L3=range(120,220,2);from itertools import ifilterfalse, chain' \
      'L4 = list(ifilterfalse(frozenset(chain(L2, L3)).__contains__, L1))' 
    10000 loops, best of 3: 47.1 usec per loop
    

    So I believe that the answer I gave last night is still far and away (for values of “far and away” greater than around 5µsec, obviously) the best, unless the questioner will have duplicates in L1 and wants them removed once each for every time the duplicate appears in one of the other lists.

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

Sidebar

Related Questions

I have several unordered lists that I want to display like this: <ul> <li><img></li>
I have three lists that I want to convert into one list. When I
I have several lists in Matlab that I want to write to the same
I have two employee lists that I want to get only unique records from
I have a list that I want to embed within another list. However, jQuery
Hi I am wondering how to create dynamic drop down lists that get refined
I created the list that have device detected, and I want to send data
I have several different lists I want to call. They all have the same
I have multiple SharePoint lists and want to display data from them on to
I have two lists which are guaranteed to be the same length. I want

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.