Data structures are of crucial importance in a programming language. How to store, manage, and manipulate data are the key factors in creating robust and efficient programs.
One of the built-in data structures in Python is list, which is represented as a collection of data points in square brackets. Lists can be used to store any data type or a mixture of different data types.
Here is an example of a Python list:
mylist = ["a", "a", "b", "c", "c", "c", "c", "d", "d"]
In this article, we will learn 3 ways of counting the items in a list, which might be a very useful input for a lot of tasks. To be more specific, we will count the number of occurrences of items to obtain a distribution of them. Otherwise, the total number of items can easily be calculated using the len function.
len(mylist)
# output
9
Count method
The first one is the count method, which returns the number of occurrences of the given item.
mylist = ["a", "a", "b", "c", "c", "c", "c", "d", "d"]
mylist.count("c")
# output
4
This method is useful if you are looking for a specific item. To get the frequency of all items at once, we can use one of the following two ways.
Counter function
The counter function in the collections module can be used for finding the frequencies of items in a list. It is a dictionary subclass and used for counting hashable objects.
Counter returns a counter object in which items are stored as keys and the frequencies are stored as values. It is quite similar to a Python dictionary, which is another built-in data structure.
from collections import Counter
mylist = ["a", "a", "b", "c", "c", "c", "c", "d", "d"]
Counter(mylist)
# output
Counter({'a': 2, 'b': 1, 'c': 4, 'd': 2})
We can access the number of occurrences of an item as follows:
mycounter = Counter(mylist)
mycounter["a"]
# output
2
The counter function can also be used to count the characters in a string. Here is an example:
mystring = "Data Science ????"
Counter(mystring)
# output
Counter({'D': 1,
'a': 2,
't': 1,
' ': 2,
's': 1,
'c': 2,
'i': 1,
'e': 2,
'n': 1,
'?': 4})
Pandas value counts
The value counts function of Pandas returns all the items along with their number of occurrences. As a result, we get an overview of the distribution of the list.
Since it’s a Pandas function, we first need to convert the list to a Series.
import pandas as pd
mylist = ["a", "a", "b", "c", "c", "c", "c", "d", "d"]
pd.Series(mylist).value_counts()
# output
c 4
a 2
d 2
b 1
dtype: int64
The output of the value counts function is a Series whose index contains the items in the list. We can access the number of occurrences of an item as follows:
myseries = pd.Series(mylist).value_counts()
myseries["d"]
# output
2
We can also get the percent share of the items using the normalize parameter.
pd.Series(mylist).value_counts(normalize=True)
# output
c 0.444444
a 0.222222
d 0.222222
b 0.111111
dtype: float64
In this short guide, we have learned 3 different ways of performing a simple yet important operation in Python.
Thank you for reading. Please let me know if you have any feedback.