Python | Find sum of frequency of given elements in the list
Last Updated :
24 May, 2023
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()
Python3
# 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()
Python3
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:
Python3
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
OutputInitial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Method #4: Using operator.countOf() method
Python3
# 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)
OutputInitial 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.
Python3
# 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.
Similar Reads
Python - Step Frequency of elements in List Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python - Count frequency of Sublist in given list Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
3 min read
Python - Fractional Frequency of elements in List Given a List, get fractional frequency of each element at each position. Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 4]Â Output : ['1/4', '1/3', '2/4', '1/1', '1/1', '2/3', '3/4', '3/3', '4/4']Â Explanation : 4 occurs 1/4th of total occurrences till 1st index, and so on.Input : test_list = [4, 5, 4,
5 min read
Python - Find the frequency of numbers greater than each element in a list Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3,
8 min read
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read