Python | Count of common elements in the lists
Last Updated :
09 May, 2023
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
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(1).
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
Time complexity: O(n)
Auxiliary space: O(1)
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))
OutputThe 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
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) additional space is not needed
Method#4: Using set intersection
Algorithm:
- Initialize two lists, test_list1 and test_list2, with the given values.
- Use the set intersection operator to get a set of common elements from both lists.
- Get the length of the set obtained from the above step.
- Assign the obtained value to res variable.
- Print the result.
Python3
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
# using set intersection to get number of identical elements
res = len(set(test_list1) & set(test_list2))
# printing result
print("Summation of Identical elements : " + str(res))
OutputSummation of Identical elements : 4
Time complexity: O(n), where n is the length of the input lists. This is because we are only iterating over the two input lists once.
Auxiliary space: O(m), where m is the length of the set of common elements between the two input lists. This is because we are creating a set to hold the common elements between the two input lists.
Method #5: Using dictionary
- Initialize an empty dictionary freq_dict.
- Iterate through test_list1 and add the element as a key to freq_dict with a value of 1 if it doesn't already exist, otherwise increment the existing value by 1.
- Iterate through test_list2, for each element, if it exists in freq_dict, decrement its value by 1 and add it to the result variable res. If its value becomes 0, delete the key from freq_dict.
- Print the value of res.
Python3
# Python3 code to demonstrate working of
# Identical element summation in lists
# using dictionary
# 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 dictionary
freq_dict = {}
res = 0
for i in test_list1:
if i not in freq_dict:
freq_dict[i] = 1
else:
freq_dict[i] += 1
for i in test_list2:
if i in freq_dict and freq_dict[i] > 0:
res += 1
freq_dict[i] -= 1
# printing result
print("Number of Identical elements : " + str(res))
OutputThe original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Number of Identical elements : 4
Time complexity: O(n)
Auxiliary space: O(n)
Similar Reads
Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides
3 min read
Count occurrences of an element in a list in Python A common task when working with lists is to count how many times a specific element appears. In Python, we have several ways to count occurrences of an element using both built-in and custom methods.The simplest and most straightforward way to count occurrences of an element in a list is by using th
2 min read
Python | Count occurrences of an element in a Tuple In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple:
3 min read
Python - Check if list contains all unique elements To check if a list contains all unique elements in Python, we can compare the length of the list with the length of a set created from the list. A set automatically removes duplicates, so if the lengths match, the list contains all unique elements. Python provides several ways to check if all elemen
2 min read
Python | Intersection of two lists The task of finding the intersection of two lists involves identifying the common elements between them. This means we want to extract the values that appear in both lists, while ignoring any duplicates or values that are unique to each list. For example, if we have two lists [4, 9, 1, 17, 11] and [
4 min read
Count distinct elements in an array in Python Given an unsorted array, count all distinct elements in it. Examples: Input : arr[] = {10, 20, 20, 10, 30, 10} Output : 3 Input : arr[] = {10, 20, 20, 10, 20} Output : 2 We have existing solution for this article. We can solve this problem in Python3 using Counter method. Approach#1: Using Set() Thi
2 min read