# Python List count() method

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

## 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:

• 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.

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

Scroll to Top