How to Count Occurrences in a Python List

Lists of various types are a common part of programs. A python list can be an array, data frame, even a string. Sometimes, you want to know how many occurrences of a given element value are found within a list. Fortunately, in Python code, this requires you to use a very simple formula.

What is this function?

If you want a python count occurrences in list, you need to use the count() function. It is a very simple function with only a single argument. It takes the form of a list.count(argument) and the argument can be any data type. The count() function provides the count occurrence of the argument of the function, within a given list. The number produced is simply the number of times the argument occurs within the list item. What this number means depends entirely upon the context of the data within the linked list.

The method of the count() function is to search the values of each particular element of the list to find and count the unique values being looked for to find the number of duplications. It would be the same as personally visually going to the list comprehension and looking for a particular value while counting all duplications. A computer does this a lot faster with the python list count function but the process is basically the same. You can even use a for loop to do the same job while python programming. In such a case, you would have the computer go through the list one element at a time to check its total occurrence value or to find missing values without the python list count function.

What does it look like?

Here is a basic code example of the count() function in action.

# python count occurrences in list
> q = [1,2,2,3,3,3,4]
> x = q.count(2) > print(x)
2
> x = q.count(3)
> print(x)
3
> x = q.count(4)
> print(x)
1
> x = q.count(5)
> print(x)
0
> x = q.count('h')
> print(x)
0

Note that even when a string is being searched for, no error message results but you simply get a zero. The next example is an array of individual characters.

> q = ['a','c','a','d','a','z']
> x= q.count('a')
> print(x)
3
> x = x= q.count(3)
> print(x)
0

Once again would you do not get an error message despite the fact that in the last case we enter the number. This function even works With am input string, when searching for a substring to count.

> p = 'do the right thing!'
> x = p.count('t') > print(x)
3
> x = p.count('z')
> print(x)
0
> x = p.count(5)
TypeError: must be str, not int

In this case, when we entered a number we got an error message, this is because in with an input string, it does not accept the number but only a string.

The applications of the count() function are many. One example would be converting a list that includes demographic information into numbers counting those demographics. It can be used in any case where you are counting the contents of a list, dataframe, or file, and this includes the number of students that got a particular grade, the number of various types of cars, and many other things. Anytime you need to count the contents of a list the Python count() function is here for you. As with any python tutorial, the goal is to learn how to use the python counter function being described. This is a very simple regular expression to learn and use, once you get the hang of it will be a very useful tool you can use in any Python program to fix your python string count number problem.

How to Count Occurrences in a Python List

Leave a Reply

Your email address will not be published.

Scroll to top