There are two lists and I want to check how many of elements are duplicate. Assuming list one is l1 = ['a', 'b', 'c', 'd', 'e']
and list two is l2 = ['a', 'f', 'c', 'g']
. Since a
and c
are in both lists, therefore, the output should be 2
which means there are two elements that repeated in both lists. Below is my code and I want to count how many 2
are in counter. I am not sure how to count that.
l1 = ['a', 'b', 'c', 'd', 'e']
l2 = ['a', 'f', 'c', 'g']
from collections import Counter
c1 = Counter(l1)
c2 = Counter(l2)
sum = c1+c2
z=sum.count(2)
What you want is set.intersection
(if there are no duplicates in each list):
l1 = ['a', 'b', 'c', 'd', 'e']
l2 = ['a', 'f', 'c', 'g']
print(len(set(l1).intersection(l2)))
Output:
2