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

  • Home
  • SEARCH
  • 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 want to create a list of columns in SQL Server 2005 that have
I have a list that I want to embed within another list. However, jQuery
I have two lists of custom objects and want to update a field for
Lists in C# have the .ToArray() method. I want the inverse, where an array
Say I have a list and I want it arranged so that the sum
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
I have two string lists that I'm working with. One that has a list
It seems that if I want to create a very basic Cocoa application with
I want to create generic function that will need only parameter as Stored procedure
In a database we create indexes on columns that we want to query with

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.