Python List count() method

In this article, we will dsicuss the count() method of List class in Python.

Table Of Contents

Introduction to List.count() method

In Python, List class provides a member function count(), to get the occurrence count of any element in the list.

Syntax of List count()

The syntax of count() function is as follows,

list.count(element)

Arguments:

Advertisements
  • element – The element that needs to be counted.

Returns

  • It returns the occurrence count of the given element in the list. If the given element does not exist in the list, then it will return 0.

Let’s see some examples,

Example 1 of List.count()

Suppose we have a list of numbers, and we want to find the occurrence count of number 15 in this list. To do that, pass the number 15 in the count() function. Like this,

# List of numbers
listOfNums = [19, 15, 19, 11, 15, 15, 12, 11, 19, 15, 19, 16, 15, 19, 16, 15, 15]

# Get the occurrence count of number 15 in the list
occurrenceCount = listOfNums.count(15)

print("Occurrence count of 15 is :", occurrenceCount)

Output:

Occurrence count of 15 is : 7

It returned the occurrence count of number 15 in the list.

Example 2 of List.count()

Let’s see another example, where we will try to find the occurrence count of an element that does not exist in the list.

# List of numbers
listOfNums = [19, 15, 19, 11, 15, 15, 12, 11, 19, 15, 19, 16, 15, 19, 16, 15, 15]

# Get the occurrence count of number 20 in the list
occurrenceCount = listOfNums.count(20)

print("Occurrence count of 20 is :", occurrenceCount)

Output:

Occurrence count of 20 is : 0

As element 20 does not exists in the list, therefore the count() function returned 0.

Example 3 of List.count()

Let’s see another example, where we will try to find the occurrence count of a string in a list of strings.

# List of strings
listOfStrings = ["is", "why", "is", "where", "is", "is", "that", "this"]

# Get the occurrence count of string "is" in the list
occurrenceCount = listOfStrings.count("is")

print("Occurrence count of string 'is' :", occurrenceCount)

Output:

Occurrence count of string 'is' : 4

It returned the occurrence count of string “is” in the list.

Summary

We learned all about the count() method of List class in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top