Python List count() Method Tutorial

Python List Count() Method

This is a detailed tutorial of the Python List count() method. Learn to find the number of occurrences of a particular element in a list using this method.

Python List count()

The list.count() method is used to count the number of occurrences of a particular element or item in a list. The item for which you want to find out the number of occurrences has to be provided as the only argument of this method. This method returns the count as an integer number.

Syntax

The following syntax will find out the occurrences of the item in the list and returns the count as an integer number.

count() Parameters

The Python List count() method only takes one argument and that is the item for which the occurrences are to be looked up.

count() Return Value

The method returns an integer value and that is the number of items, the item passed as argument is present in the list. In case, the item passed in the argument is not present in the list, this method returns 0.

Examples

Two examples demonstrating the usage of the list.count() method are given below with code and output screenshots.

Example 1. Count the number of occurrences of an item in a Python List

Here I’ve defined a list with English Alphabet letters as its items. Some of the letters have multiple occurrences.

Then I’ve used the list.count() method to find out the number of occurrences of the letter 'a', 'b' and 'm'.

Python List Count() Method Example

You can see as the letter 'm' is not present in the list, this method returned 0.

Example 2. Count the occurrence of different type of collection objects in a list

In the following code snippet, there’s a list named collections containing different collection objects (Set, Tuple, List) as its items.

Here also, I’ve used the count() method to find the number of occurrences of each of these collection objects, one by one.

Python List Count() Method Example Finding Count Of Collection Objects In A List

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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