Python count frequency in list
In this post, you will learn how to find the frequency of elements in a list using Python programming. There are different ways to achieve this. Here we have mentioned two of them.
A list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow, and there is no need to declare it.
Count frequency in list using for loop
We can count the frequency of each element in a list using a for loop and a dictionary. We create an empty dictionary that will contain unique elements from the input list as keys and their counts as values. First, we initialise the list with elements and an empty dictionary, and then iterate over the list of elements. Inside the loop, we check whether the element is present in the dictionary or not. If the element is already present in the dictionary, then increase its count. And, if the element is not present in the dictionary, then initialise its count with 1.
# Python program to count the frequency of
# elements in a list using a dictionary
# initializing list
x_list = ['23', '23', '42', '27', '42', '27', '27', '71', '62']
dictionary = {}
# iterating over the list
for item in x_list:
# checking the element in dictionary
if item in dictionary:
# incrementing the counr
dictionary[item] += 1
else:
# initializing the count
dictionary[item] = 1
# printing the frequency
print(dictionary)
Output of the above code:
{'23': 2, '42': 2, '27': 3, '71': 1, '62': 1}
Count frequency in list using collections module
The Collections module in Python contains containers which are used to store collections of data, for example, lists, dicts, sets, tuples, etc. First, install the collections module using the following command-
In the given example, we import the collection module and initialise the list with elements. The Python Counter class is part of the Collections module. It is a subclass of the dictionary. It stores elements as dictionary keys, and their counts are stored as dictionary values. By using this, we can get the frequency of each element in a list. Finally, we convert the result to a dictionary using the dict and print the frequency.
# Python program to count the frequency of
# elements in a list using collections module
# importing the module
import collections
# initializing list
x_list = ['19', '19', '42', '27', '42', '27', '27', '19', '27']
# using Counter to find frequency of elements
frequency = collections.Counter(x_list)
# printing the frequency
print(dict(frequency))
Output of the above code:
{'19': 3, '42': 2, '27': 4}
Related Articles
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module