- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

A given list has many repeated items. We are interested in finding out the sum of the frequency of some such items which are repeated in the list. Below are the approaches how we can achieve this.

## With sum

We have two lists. One has the list of values and other has the values whose frequency needs to be checked from the first list. So we create a for loop to count the number of occurrences of the elements from the second list in the first list and then apply the sum function to get the final sum of frequency.

## Example

chk_list= ['Mon', 'Tue'] big_list = ['Mon','Tue', 'Wed', 'Mon','Mon','Tue'] # Apply sum res = sum(big_list.count(elem) for elem in chk_list) # Printing output print("Given list to be analysed: \n", big_list) print("Given list to with values to be analysed:\n", chk_list) print("Sum of the frequency: ", res)

## Output

Running the above code gives us the following result −

Given list to be analysed: ['Mon', 'Tue', 'Wed', 'Mon', 'Mon', 'Tue'] Given list to with values to be analysed: ['Mon', 'Tue'] Sum of the frequency: 5

## With collections.Counter

The Counter function from collections module can get the desired result by applying it to the list whose values have to be analysed while looping through the smaller list which has only the elements whose frequency needs to be established.

## Example

from collections import Counter chk_list= ['Mon', 'Tue'] big_list = ['Mon','Tue', 'Wed', 'Mon','Mon','Tue'] # Apply Counter res = sum(Counter(big_list)[x] for x in chk_list) # Printing output print("Given list to be analysed: \n", big_list) print("Given list to with values to be analysed:\n", chk_list) print("Sum of the frequency: ", res)

## Output

Running the above code gives us the following result −

Given list to be analysed: ['Mon', 'Tue', 'Wed', 'Mon', 'Mon', 'Tue'] Given list to with values to be analysed: ['Mon', 'Tue'] Sum of the frequency: 5

- Related Articles
- List frequency of elements in Python
- Python – Fractional Frequency of elements in List
- Find sum of elements in list in Python program
- Python program to find sum of elements in list
- Python – Count frequency of sublist in given list
- Find frequency of given character at every position in list of lists in Python
- Python – Restrict Elements Frequency in List
- Program to find sum of odd elements from list in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find sum of non-adjacent elements in a circular list in python
- How to find the sum of two list elements in R?
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Find all triplets in a list with given sum in Python
- Get positive elements from given list of lists in Python
- Program to find maximum sum of popped k elements from a list of stacks in Python