Python List count() Method Tutorial

Python list contains multiple items in a single variable. These items’ values can be the same or different. The List count() method is provided by all list types in order to count the specified value.

List count() Syntax

The list count() method has the following syntax where it accepts a single parameter that is required.

LIST.count(VALUE,START,END)
  • LIST is the list where the search will do.
  • VALUE is the value or variable which will be searched in LIST.
  • START is optional parameter used to set start index for the VALUE.
  • END is optional parameter used to set end index for the VALUE.

Count Specified Value/Item In List

In the following example, we will find the count of the value “ismail” in the list named “names”.

names = ["ismail","ahmet","ali","ismail","ahmet","ismail"]

count = names.count("ismail")

print(count)

The count() method is provided for list type even if the list is not a variable. In the following example, we will call the count() method for the list value.

count = ["ismail","ahmet","ali","ismail","ahmet","ismail"].count("ismail")

print(count)

The output will be like below which is the count of the “ismail”.

3

Count Values/Items Inside Specified Range

We can search for a value or item inside specified index range. We provide the value with the start and end index numbers. In the following example we search “ismail” between 1 and 5 .

names = ["ismail","ahmet","ali","ismail","ahmet","ismail"]

count = names.count("ismail",1,5)

print(count)
2

Leave a Comment