Open In App

Python | Find sum of frequency of given elements in the list

Given two lists containing integers, the task is to find the sum of the frequency of elements of the first list in the second list. 

Example: 



Input: list1 = [1, 2, 3]
       list2 = [2, 1, 2, 1, 3, 5, 2, 3]

Output: 7

Explanation:
No of time 1 occurring in list2 is :2
No of time 2 occurring in list2 is :3
No of time 3 occurring in list2 is :2
Sum = 2+3+2 = 7

Below are some ways to achieve the above tasks. 

Method #1: Using sum() 






# Python code to find sum of frequency of
# element of first list in second list.
 
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
 
# Using sum
Output = sum(Input2.count(elem) for elem in Input1)
 
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)

Output:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7

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

Method #2: Using sum() and Counter() 




from collections import Counter
 
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
 
 
temp = Counter(Input2)
Output = sum(temp[x] for x in Input1)
 
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)

Output:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7

Method #3: Using the reduce() function from the functools module: 

This approach involves using the reduce() function to iterate through the elements in the first list and add up the frequencies of those elements in the second list. Here’s an example of how it could be done:




from functools import reduce
 
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
 
# Find sum of frequencies using reduce()
sum_freq = reduce(lambda x, y: x + Input2.count(y), Input1, 0)
 
print("Initial list are:", Input1, Input2)
print("Frequency is:", sum_freq)
#This code is contributed by Edula Vinay Kumar Reddy

Output
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7

Method #4: Using operator.countOf() method




# Python code to find sum of frequency of
# element of first list in second list.
import operator as op
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
 
# Using sum
Output = sum(op.countOf(Input2,elem) for elem in Input1)
 
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)

Output
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7

Time Complexity: O(N), where n is the length of the given list
Auxiliary Space: O(N)

Method #5: Using filter() method 

In this example, we have initialized the test list1 and test list2. We have used the filter function on list2 which filters out the element that is not in list1. The length of the list is the final answer.




# Python code to find sum of frequency of
# element of first list in second list.\
 
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
 
# Using filter
Output = len([*filter(lambda a : a in Input1, Input2)])
 
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)

Output :

Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7

Time complexity: O(M*N), Where M is the length of list 1 and N is the length of list 2. 

Auxiliary space: O(M), Where M is the length of the filtered list. 


Article Tags :