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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:57:41+00:00 2026-06-07T18:57:41+00:00

How can i check if item in b is in a and the found

  • 0

How can i check if item in b is in a and the found match item in a should not be use in the next matching?

Currently this code will match both 2 in b.

a = [3,2,5,4]
b = [2,4,2]

for i in b:
  if i in a:
    print "%d is in a" % i

This is the required output:

2 => 2 is in a
4 => 4 is in a
2 =>

EDIT: Example 2:

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

output should be

2 => 2 is in a
4 => 4 is in a
2 => 2 is 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-07T18:57:42+00:00Added an answer on June 7, 2026 at 6:57 pm

    (long post but read it entirely, solution is at the end).

    Remove the found value or register it in another dict.

    Better though is to count the number of apparitions inside each array and test how many are common.

    For the second case, you’d have

    • for a:

      3 appears 1 times
      2 appears 1 times
      5 appears 1 times
      4 appears 1 times

    • for b:

      2 appears 2 times
      4 appears 1 times

    Keep these values in dictionaries:

    a_app = {3:1, 2:1, 5:1, 4:1}
    b_app = {2:2, 4:1}
    

    And now, it is simple:

    for i in b:
        if a_app.has_key(i) and a_app[i] > 0:
            a_app[i] -= 1
    

    The b_app dictionary would be used in other case.

    Here is a test script I wrote (testing all testcases issued here):

    def f(a, b):
        a_app = {}
        for i in a:
            if not a_app.has_key(i):
                a_app[i] = 0
            a_app[i] += 1
        print a_app
        for i in b:
            print i, '=>',
            if a_app.has_key(i) and a_app[i] > 0:
                a_app[i] -= 1
                print i, ' is in a',
            print '.'
    
    f([1,1,2],[1,1])
    f([3,2,5,4],[2,4,2])
    f([3,2,2,4],[2,4,2])
    f([3,2,5,4],[2,3,2])
    

    And here is the output:

    $ python 1.py
    {1: 2, 2: 1}
    1 => 1  is in a .
    1 => 1  is in a .
    {2: 1, 3: 1, 4: 1, 5: 1}
    2 => 2  is in a .
    4 => 4  is in a .
    2 => .
    {2: 2, 3: 1, 4: 1}
    2 => 2  is in a .
    4 => 4  is in a .
    2 => 2  is in a .
    {2: 1, 3: 1, 4: 1, 5: 1}
    2 => 2  is in a .
    3 => 3  is in a .
    2 => .
    

    Everything is perfect and no order is lost 🙂

    Edit: Updated with @Avaris’s suggestions, this script looks like:

    import collections
    
    def f(a, b):
        a_app = collections.Counter(a)
        for i in b:
            print i, '=>',
            if i in a_app and a_app[i] > 0:
                a_app[i] -= 1
                print i, ' is in a',
            print '.'
        print ''
    
    f([1,1,2],[1,1])
    f([3,2,5,4],[2,4,2])
    f([3,2,2,4],[2,4,2])
    f([3,2,5,4],[2,3,2])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target
With this code I can check the caret position in a textarea in firefox:
Can someone check whether the code below is correct? The code is found at
I've found a Perl regexp that can check if a string is UTF-8 (the
In this post i found INotifyPropertyChanged and i check it's example but I notice
This is driving me nuts! A little piece of code that I can't seem
How can I check f a keyword exists in a div? for instance, this
Can someone check a xml reply string. User said it is not XML compatible
Issue The code does not correctly identify the input (item). It simply dumps to
I may have found an odd issue today with Umbraco 4.8.1 (this is not

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.