Python - Count of matching elements among lists (Including duplicates)
Last Updated :
01 Feb, 2023
Given 2 lists, count all the elements that are similar in both the lists including duplicated.
Input : test_list1 = [3, 5, 6, 7, 2, 3, 5], test_list2 = [5, 5, 3, 9, 8, 5]
Output : 4
Explanation : 3 repeats 2 times, and 5 two times, totalling to 4.
Input : test_list1 = [3, 5, 6], test_list2 = [5, 3, 9]
Output : 2
Explanation : 3 repeats 1 time, and 5 one time, totalling to 2.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we iterate for each element of other list while iterating for one list, and increase the counter.
Python3
# Python3 code to demonstrate working of
# Count of matching elements among lists [ Including duplicates ]
# Using reduce
from functools import reduce
# initializing lists
test_list1 = [3, 5, 6, 7, 3, 2]
test_list2 = [5, 5, 3, 9, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using reduce to count the element of list
res = reduce(lambda x, y: x+test_list1.count(y), set(test_list2), 0)
# printing result
print("All matching elements : " + str(res))
OutputThe original list 1 : [3, 5, 6, 7, 3, 2]
The original list 2 : [5, 5, 3, 9, 8]
All matching elements : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Python3
# Python3 code to demonstrate working of
# Count of matching elements among lists [ Including duplicates ]
# Using sum() + generator expression
# initializing lists
test_list1 = [3, 5, 6, 7, 2, 3]
test_list2 = [5, 5, 3, 9, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using sum to count occurrences
res = sum(ele in test_list1 for ele in test_list2)
# printing result
print("All matching elements : " + str(res))
OutputThe original list 1 : [3, 5, 6, 7, 2, 3]
The original list 2 : [5, 5, 3, 9, 8]
All matching elements : 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Method#3: Using reduce + count function
The combination of above method can be used to solve this problem. In this, we count element by using reduce and count function.
Python3
# Python3 code to demonstrate working of
# Count of matching elements among lists [ Including duplicates ]
# Using reduce
from functools import reduce
# initializing lists
test_list1 = [3, 5, 6, 7, 3, 2]
test_list2 = [5, 5, 3, 9, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using reduce to count the element of list
res = reduce(lambda x, y: x+test_list1.count(y), set(test_list2), 0)
# printing result
print("All matching elements : " + str(res))
OutputThe original list 1 : [3, 5, 6, 7, 3, 2]
The original list 2 : [5, 5, 3, 9, 8]
All matching elements : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Method#4: Using Counter() function
Python3
# Python3 code to demonstrate working of
# Count of matching elements among lists [ Including duplicates ]
from collections import Counter
# initializing lists
test_list1 = [3, 5, 6, 7, 2, 3]
test_list2 = [5, 5, 3, 9, 8]
freq = Counter(test_list1)
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using sum to count occurrences
res = 0
for i in test_list2:
if i in freq.keys():
res += 1
# printing result
print("All matching elements : " + str(res))
OutputThe original list 1 : [3, 5, 6, 7, 2, 3]
The original list 2 : [5, 5, 3, 9, 8]
All matching elements : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using operator.countOf() method:
Python3
# Python3 code to demonstrate working of
# Count of matching elements among lists [ Including duplicates ]
import operator as op
# initializing lists
test_list1 = [3, 5, 6, 7, 2, 3]
test_list2 = [5, 5, 3, 9, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using sum to count occurrences
res = 0
for i in test_list2:
if op.countOf(test_list1, i) > 0:
res += 1
# printing result
print("All matching elements : " + str(res))
OutputThe original list 1 : [3, 5, 6, 7, 2, 3]
The original list 2 : [5, 5, 3, 9, 8]
All matching elements : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Python | Count of Matching i, j index elements Sometimes, while programming, we can have a problem in which we need to check for ith and jth character of each string. We may require to extract count of all strings with similar ith and jth characters. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop This is b
4 min read
Python - Difference of Two Lists Including Duplicates We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read
Python | Find common elements in list of lists The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists. Let's discuss certain ways in which this operation can be
6 min read
Python - Duplicate Element Indices in List We are having a list we need to find the duplicate element indices. For example, we are given a list a = [10, 20, 30, 20, 40, 30, 50] we need to find indices of the duplicate items so that output should be {20: [1, 3], 30: [2, 5]}.Using a loop and a dictionaryWe iterate through the list using enumer
3 min read
Python - Elements frequency count in multiple lists Sometimes while working with Python lists we can have a problem in which we need to extract the frequency of elements in list. But this can be added work if we have more than 1 list we work on. Let's discuss certain ways in which this task can be performed. Method #1: Using dictionary comprehension
6 min read
Python | Matching elements count Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the count of indices of the match. Querying whole list for the this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.
5 min read