Open In App

Python | Matching elements count

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. Let’s discuss certain ways in which this can be achieved. 

Method #1 : Using list comprehension + index() + len() This problem can potentially be solved using the index function of python to get the wanted indices and list comprehension can be used to extend this to the whole string. The len() is used to return total count of match. 

Python3




# Python3 code to demonstrate
# Matching elements count
# using list comprehension and index() + len()
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using list comprehension and index() + len()
# Matching elements count
res = len([test_list1.index(i) for i in test_list2])
 
# print result
print("The Match indices list count is : " + str(res))


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list count is : 2

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using enumerate() + len() + list comprehension The enumerate function can be used to produce the key value pair for the list being it’s index and value and we can store them using list comprehension. The len() is used to return total count of match. 

Python3




# Python3 code to demonstrate
# Matching elements count
# using list comprehension and enumerate() + len()
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using list comprehension and enumerate() + len()
# Matching elements count
res = len([key for key, val in enumerate(test_list1) if val in set(test_list2)])
 
# print result
print("The Match indices list count is : " + str(res))


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list count is : 2

Time Complexity: O(n) where n is the number of elements in the in the list “test_list”. The enumerate() + len() + list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is needed

Method #3:Using Counter() function

Python3




# Python3 code to demonstrate
# Matching elements count
from collections import Counter
res = 0
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
freq = Counter(test_list1)
for i in test_list2:
    if i in freq.keys():
        res += 1
# print result
print("The Match indices list count is : " + str(res))


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list count is : 2

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using set intersection

Python3




# Python3 code to demonstrate
# Matching elements count
# using set intersection
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using set intersection
# Matching elements count
res = len(set(test_list1) & set(test_list2))
 
# print result
print("The Match indices list count is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list count is : 2

Time Complexity: O(N)
Auxiliary Space: O(N)

This approach uses the set intersection method to find the common elements between the two lists. The intersection of two sets returns a new set containing only the elements present in both sets. The length of this new set is the count of common elements between the two lists.

Method #5: Using a dictionary and a loop

  • Initialize an empty dictionary.
  • Loop through the first list and add each element as a key to the dictionary with a value of 1.
  • Loop through the second list and check if each element is a key in the dictionary.
  • If it is, increment the value of the corresponding key in the dictionary by 1.
  • The count of matching elements is the sum of the values in the dictionary that are greater than or equal to 1.
  • Return the count.

Python3




# Python3 code to demonstrate
# Matching elements count
# using a dictionary and a loop
 
# initializing lists
test_list1 = [5, 4, 1, 3, 2]
test_list2 = [1, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# using a dictionary and a loop
# Matching elements count
dict1 = {}
for num in test_list1:
    dict1[num] = 1
count = 0
for num in test_list2:
    if num in dict1:
        dict1[num] += 1
for val in dict1.values():
    if val >= 2:
        count += 1
 
# print result
print("The Match indices list count is : " + str(count))


Output

The original list 1 : [5, 4, 1, 3, 2]
The original list 2 : [1, 2]
The Match indices list count is : 2

Time complexity: O(n+m), where n and m are the lengths of the two lists, since we loop through each list once.
Auxiliary space: O(n), where n is the length of the first list, since we store each element of the first list as a key in the dictionary.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads