Python count list

Python count list

In Python the count method on the list data structure is used to count the number of occurrences of an element in the main list, but not within any sublists. If the element is not found, the value will be 0.

Python count list – count the number of occurrences in list

We take as a reference a list of votes, then we apply the method count to find how many times the number 7 appears, for example. We store this value in a variable and finally we print the result.

votes = [9,7,7,10]
count_votes = votes.count (7)
print (count_votes)

In this case, the value 2 will be output, i.e. the number of times the number 7 is required.

Python count list – count an element that is not in the list

Now let’s try to count a missing element. So what will we receive in output?

votes = [9,7,7,10]
count_votes = votes.count (6)
print (count_votes)

In this case the value 0 will be output.

Try the code in the online compiler that you find at the following link: online Python compiler.

Python count list – on a list of strings

We create a list of names, then apply the count method to search for a name. Then we print the obtained value.

names = [‘Cristina’, ‘Tom’, ‘Cristina’, ‘Jessica’]
count_names = names.count (‘Cristina’)
print (count_names)

What changes is just that we have to be careful about putting quotes in the count method.

If the name does not exist, the value 0 will always be printed.

Attention, if we enter the name with a lowercase initial we will clearly not find it and 0 will be returned.

Python count list – use of the variable

We ask you to enter a name as input and then we store it in a varibial. Then we search for it using the count method.

name = input (‘Insert a name’)
names = [‘Cristina’, ‘Tom’, ‘Cristina’, ‘Jessica’]
count_names = names.count (name)
print (count_names)

We can thus enter a different name from time to time.


In this lesson we studied the count method on the list data structure in Python, creating very simple examples. In the next lesson we will study the index method.

Some useful links

Python tutorial

Python Compiler

Install Python


Assignment operators



How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot

Leave a Reply

Your email address will not be published. Required fields are marked *