Skip to content
Related Articles

Related Articles

Python | Count of common elements in the lists

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes, while working with Python list we can have a problem in which we have to compare two lists for index similarity and hence can have a task of counting equal index pairs. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using sum() + zip() This task can be performed by passing the zip(), which performs task of mapping both list with each other, to the sum() which computes the sum according to equal indices. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using sum() + zip()
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using sum() + zip()
res = sum(x == y for x, y in zip(test_list1, test_list2))
 
# printing result
print("Summation of Identical elements : " + str(res))

Output : 

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

Method #2 : Using sum() + map() + eq The task performed in above method using zip() can be executed here using the map function which performs similar task. The equality check can be performed by inbuilt eq operator. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using sum() + map() + eq
from operator import eq
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using sum() + map() + eq
res = sum(map(eq, test_list1, test_list2))
 
# printing result
print("Summation of Identical elements : " + str(res))

Output : 

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

Method #3: Using set() + len() The task performed can also be perform using the set and len function. We can get a common element using set intersection and count the total common element using len function. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using set() + let()
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using set() + len()
res = len(set(test_list1) & set(test_list2));
 
# printing result
print("Summation of Identical elements : " + str(res))

Output

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!