Count Frequency of Words in List in Python (3 Examples)
In this tutorial, you will learn how to count the number of words in a list in the Python Programming Language.
The article contains the following content:
Let’s dive right in!
Creation of Example Data
At the start, we will define a sample list containing 7 strings of words. See my_list defined below.
my_list=['car', 'chair', 'car', 'boat', 'boat', 'clock', 'apple'] print(my_list) # ['car', 'chair', 'car', 'boat', 'boat', 'clock', 'apple']
Let’s check the ways of counting the occurrence of each word in a list in the examples below.
Example 1: Count Frequency of Words in List Using collections Module
In this example, we will show how to use Counter class of the collections module to count the frequencies of words in a list. To do so, first, we need to import the Counter class from this module.
from collections import Counter
Now, we can plug my_list into the Counter constructor and assign it to a Counter container/object named counts.
counts = Counter(my_list) print(counts) # Counter({'car': 2, 'boat': 2, 'chair': 1, 'clock': 1, 'apple': 1})
The output of Example 1 shows that the words “car” and “boat” appear twice in our list, while “chair”, “clock” and “apply”‘ appear only once.
Example 2: Count Frequency of Words in List Using pandas & NumPy Libraries
It is also possible to return the frequency of words in a list using the pandas and NumPy libraries. For the implementation, we will need to import the libraries first.
import numpy as np import pandas as pd
Now, we can use the value_counts() function of pandas together with the array() function of NumPy to obtain how many times each word appears in my_list.
pd.value_counts(np.array(my_list)) # car 2 # boat 2 # chair 1 # clock 1 # apple 1
The previous output gives the same result as Example 1, however, this time it has a different data type called pandas series.
Example 3: Count Frequency of Words in List Using count Function
This example explains how to use the count() function to obtain the frequency of a selected word on our list. Consider the code below for the example.
my_list.count("boat") # 2
As shown, the word “boat” appears twice in our list. We can repeat this process with every element in my_list.
Video, Further Resources & Summary
Do you need more explanations on how to count the frequency of words in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Besides that, there are other tutorials on Statistics Globe you could have a look at:
- Count Duplicates in List in Python
- Convert List from Character String to Integer in Python
- Access List Element by Index in Python
- Introduction to Python
In this article, you have learned how to get the frequency of each word in a list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter