The count method

The count method returns the number of matches of an element in the list. In the method parameter, we pass the element we want to find.

Syntax

pythonlist.count(what are we looking for)

Example

Let's find out how many times a string appears in our list:

pythonlst = ['a', 'b', 'c', 'd', 'e']
print(lst.count('a'))

Result of code execution:

python2

Example

Now let's find the missing element in the list:

pythonlst = ['a', 'b', 'c', 'd', 'e']
print(lst.count('x'))

Result of code execution:

python0

See also

  • method index,
    which searches for an element in a list and returns its index
  • method pop,
    which removes an element by its index