
- 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
Count frequencies of all elements in array in Python using collections module
As python allows duplicate elements in a list we can have one element present multiple Times. The frequency of elements in a list indicates how many times an element occurs in a list. In this article we use the Counter function of the collections module to find out the frequency of each item in a list.
Syntax
Syntax: Counter(list) Where list is an iterable in python
Example
The below code uses the Counter() to keep track of frequency and items() to iterate over each item in the result of counter function for printing in a formatted manner.
from collections import Counter list = ['Mon', 'Tue', 'Wed', 'Mon','Mon','Tue'] # Finding count of each element list_freq= (Counter(list)) #Printing result of counter print(list_freq) # Printing it using loop for key, value in list_freq.items(): print(key, " has count ", value)
Output
Running the above code gives us the following result −
Counter({'Mon': 3, 'Tue': 2, 'Wed': 1}) Mon has count 3 Tue has count 2 Wed has count 1
- Related Articles
- Count frequencies of all elements in array in Python\n
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Counting frequencies of array elements in C++
- Array elements with prime frequencies in C++?
- Array elements with prime frequencies?
- Find all elements count in list in Python
- Count array elements that divide the sum of all other elements in C++
- Count distinct elements in an array in Python
- Count occurrence of all elements of list in a tuple in Python
- Replace All Elements Of ArrayList with with Java Collections
- Rank of All Elements in an Array using C++
- Reverse order of all elements of ArrayList with Java Collections
- Deep count of elements of an array using JavaScript
- How to count unique elements in the array using java?
- Check if all array elements are distinct in Python

Advertisements