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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:10:07+00:00 2026-06-12T19:10:07+00:00

I have two lists, let’s say: a = [1,2,3] b = [1,2,3,1,2,3] I would

  • 0

I have two lists, let’s say:

a = [1,2,3]
b = [1,2,3,1,2,3]

I would like to remove 1, 2 and 3 from list b, but not all occurrences. The resulting list should have:

b = [1,2,3]

I currently have:

for element in a:
    try:
        b.remove(element)
    except ValueError:
        pass

However, this has poor performance when a and b get very large. Is there a more efficient way of getting the same results?

EDIT

To clarify ‘not all occurrences’, I mean I do not wish to remove both ‘1’s from b, as there was only one ‘1’ in a.

  • 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-12T19:10:09+00:00Added an answer on June 12, 2026 at 7:10 pm

    I would do something like this:

    from collections import defaultdict
    
    a = [1, 2, 3]
    b = [1, 2, 3, 1, 2, 3]
    
    # Build up the count of occurrences in b
    d = defaultdict(int)
    for bb in b:
        d[bb] += 1
    
    # Remove one for each occurrence in a
    for aa in a:
        d[aa] -= 1
    
    # Create a list for all elements that still have a count of one or more
    result = []
    for k, v in d.iteritems():
        if v > 0:
            result += [k] * v
    

    Or, if you are willing to be slightly more obscure:

    from operator import iadd
    
    result = reduce(iadd, [[k] * v for k, v in d.iteritems() if v > 0], [])
    

    defaultdict generates a count of the occurrences of each key. Once it has been built up from b, it is decremented for each occurrence of a key in a. Then we print out the elements that are still left over, allowing them to occur multiple times.

    defaultdict works with python 2.6 and up. If you are using a later python (2.7 and up, I believe), you can look into collections.Counter.


    Later: you can also generalize this and create subtractions of counter-style defaultdicts:

    from collections import defaultdict
    from operator import iadd
    
    a = [1, 2, 3, 4, 5, 6]
    b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    def build_dd(lst):
        d = defaultdict(int)
        for item in lst:
            d[item] += 1
        return d
    
    def subtract_dd(left, right):
        return {k: left[k] - v for k, v in right.iteritems()}
    
    db = build_dd(b)
    da = build_dd(a)
    result = reduce(iadd,
                    [[k] * v for k, v in subtract_dd(db, da).iteritems() if v > 0],
                    [])
    
    print result
    

    But the reduce expression is pretty obscure now.


    Later still: in python 2.7 and later, using collections.Counter, it looks like this:

    from collections import Counter
    
    base = [1, 2, 3]
    missing = [4, 5, 6]
    extra = [7, 8, 9]
    a = base + missing
    b = base * 4 + extra
    
    result = Counter(b) - Counter(a)
    print result
    assert result == dict([(k, 3) for k in base] + [(k, 1) for k in extra])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists. They are sortable but not connected (elements of list one
let's say I have two lists: a = list(1,2,3) b = list(4,5,6) So I
Let's say I have a list somewhere called majorPowers which contain these two lists:
I have two generic lists. Let's say they are List< A > and List<
Let's say I have a list of dicts. I define duplicates as any two
I have two Lists, they look like this <List> ads [0] Headline = Sony
Let's say you have two lists, L1 and L2, of the same length, N.
Let's say I have two generic lists of the same type. How do I
Let's say I have two lists of strings: a = ['####/boo', '####/baa', '####/bee', '####/bii',
Let's say I have a list, and a filtering function. Using something like >>>

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.