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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:08:26+00:00 2026-06-14T13:08:26+00:00

First of all, I’m new to programming and python, I’ve looked here but can’t

  • 0

First of all, I’m new to programming and python, I’ve looked here but can’t find a solution, if this is a stupid question though please forgive me!

I have two lists and I’m trying to determine how many times items in the second list appears in the first list.

I have the following solution:

    list1 = ['black','red','yellow']
    list2 = ['the','big','black','dog']
    list3 = ['the','black','black','dog']
    p = set(list1)&set(list2)
    print(len(p))

It works fine apart from when the second list contains duplicates.

i.e. list1 and list2 above returns 1, but so does list1 and list3, when ideally that should return 2

Can anyone suggest a solution to this? Any help would be appreciated!

Thanks,

Adam

  • 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-14T13:08:27+00:00Added an answer on June 14, 2026 at 1:08 pm

    You’re seeing this problem because of you’re using sets for your collection type. Sets have two characteristics: they’re unordered (which doesn’t matter here), and their elements are unique. So you’re losing the duplicates in the lists when you convert them to sets, before you even find their intersection:

    >>> p = ['1', '2', '3', '3', '3', '3', '3']
    >>> set(p)
    set(['1', '2', '3'])
    

    There are several ways you can do what you’re looking to do here, but you’ll want to start by looking at the list count method. I would do something like this:

    >>> list1 = ['a', 'b', 'c']
    >>> list2 = ['a', 'b', 'c', 'c', 'c']
    >>> results = {}
    >>> for i in list1:
            results[i] = list2.count(i) 
    >>> results
    {'a': 1, 'c': 3, 'b': 1}
    

    This approach creates a dictionary (results), and for each element in list1, creates a key in results, counts the times it occurs in list2, and assigns that to the key’s value.

    Edit: As Lattyware points out, that approach solves a slightly different question than the one you asked. A really fundamental solution would look like this

    >>> words = ['red', 'blue', 'yellow', 'black']
    >>> list1 = ['the', 'black', 'dog']
    >>> list2 = ['the', 'blue', 'blue', 'dog']
    >>> results1 = 0
    >>> results2 = 0
    >>> for w in words:
            results1 += list1.count(w)
            results2 += list2.count(w)
    
    >>> results1
    1
    >>> results2
    2
    

    This works in a similar way to my first suggestion: it iterates through each word in your main list (here I use words), adds the number of times it appears in list1 to the counter results1, and list2 to results2.

    If you need more information than just the number of duplicates, you’ll want to use a dictionary or, even better, the specialized Counter type in the collections modules. Counter is built to make everything I did in the examples above easy.

    >>> from collections import Counter
    >>> results3 = Counter()
    >>> for w in words:
            results3[w] = list2.count(w)
    
    >>> results3
    Counter({'blue': 2, 'black': 0, 'yellow': 0, 'red': 0})
    >>> sum(results3.values())
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all there is probably a question like this already but i couldn't
First of all there is a partial question regarding this, but it is not
First of all, apologize because I have seen some posts about this, but I
First of all, I am sorry if this question doesn't belong to SO since
First of all this is all just concept, I have no actual programming done
First of all, this isn't for a keylogger, it's for an input in a
First of all, I'm quite new to the Android and JAVA world (coming from
First of all I've gone through dozens of posting here on SO and google
I tried in different ways to accomplish this but I can make it work
First of all a statement: I'm a newbie when it comes to programming for

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.