Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Find Element Occurrences in List – count() Function with Examples

count() function is used to count the number of times an item (x – parameter) exists in a list. This method is a part of python programming language. It is used to find the occurrence of an element in a list.

There is a list (1, 2, 5, 8, 9, 5, 4, 5) and we have to count the element ‘5‘ in this list. We can easily do that by using count() method. count() method counts the ‘5‘ appearance from the list and return 3 as an output.

count() method can work with any type of data type.

Syntax of count() Function

The syntax of count() function in Python is:

 

Parameters of count() Function in Python

x – Where x is any valid element of the list which occurrence we want to calculate. This parameter is required.

 

Compatibility

count() function is available and compatible with both Python 2.x and 3.x. This function is one of python list methods.

 

Return Value of count() Function in Python

count() function returns the occurrence of given input.

 

Python count() Function Example 1

Here we have a list and we are going to count he number of times ‘4’ appears in this list.

Output:
4
So, four times 4 appear in above list.

 

Python count() Function Example 2

Now, there are few colors in which we have to count the blue.

Output:
3
There are three times blue color appears in the list.

Loading...
LearnAndLearn.com © 2018 - All rights reserved