Python list.count() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
list method

Definition

count(value) returns an int: how many items in the list are equal to value. If none are, the answer is 0; it never raises for a missing value. The list is not changed.

Items are compared with ==, so equal numbers match across types: 1, 1.0, and True all count as each other. Only top-level items are checked; count() does not look inside nested lists or inside strings.

list.count() is an instance method of the built-in list type: call it on a list with dot notation, such as votes.count(...). No import is needed.

Syntax

Pass exactly one value to count.

python
n = lst.count(value)
CallWhat it does
[1, 2, 1, 1].count(1)Returns 3.
["a", "b"].count("z")Returns 0; a missing value is not an error.
[[1, 2], [1]].count(1)Returns 0: the 1s are inside nested lists, not items of the outer list.

Every call scans the whole list. To count every distinct value at once, collections.Counter(lst) does it in a single pass. To count characters in one string, use str.count() instead.

Method signatures
python
list.count(value)
# returns: int, the number of items equal to value

Parameters

ParameterDescription
valueAny object, compared with each item using ==. It is required: lst.count() with no argument raises TypeError.

Return Value

Return ValueDescription
intThe number of matching items, from 0 up to len(lst).

Python Examples

Example 1 — Tally a Team Lunch Poll

Count one dish, a dish nobody picked, and then every dish in alphabetical order.

python
votes = ["pizza", "sushi", "pizza", "tacos", "pizza", "sushi"]

print(votes.count("pizza"))
print(votes.count("burgers"))

for dish in sorted(set(votes)):
    print(dish, votes.count(dish))

How It Works

  1. Exact matches. "pizza" appears three times among the six votes.
  2. Zero, not an error. Nobody voted for burgers, so count() returns 0; compare index(), which would raise ValueError.
  3. Each dish once. set(votes) drops duplicates and sorted() puts the dishes in a fixed alphabetical order for printing.
  4. A small tally. Counting each unique dish gives the full result: 3, 2, and 1.

Example 2 — Draw a Histogram of Product Star Ratings

Count each star level from 5 down to 1 and print a bar of asterisks, then the share of 5-star reviews.

python
ratings = [5, 4, 5, 3, 5, 4, 1, 5, 2, 4]

for stars in range(5, 0, -1):
    n = ratings.count(stars)
    print(stars, "*" * n, n)

share = ratings.count(5) / len(ratings)
print(f"5-star share: {share:.0%}")

How It Works

  1. Counting down. range(5, 0, -1) yields 5, 4, 3, 2, 1, so the best ratings are listed first.
  2. Bars from counts. Multiplying "*" by the count draws a bar as long as the number of reviews at that level.
  3. Share of the total. 4 of the 10 ratings are 5 stars, and the .0% format turns 0.4 into "40%".

Example 3 — Count Yes Answers Collected From Mixed Sources

One form sent 1, another True, another 1.0, and one the text "1". See which ones count() treats as equal, and what it does with nested rows.

python
answers = [1, True, 1.0, "1", 0, False]

print(answers.count(1))
print(answers.count(0))
print(answers.count("1"))

grid = [[1, 0], [1, 0], [0, 1]]
print(grid.count([1, 0]), grid.count(1))

How It Works

  1. Numbers and booleans match. 1, True, and 1.0 are all equal to 1 with ==, so the count is 3.
  2. Same for zero. 0 and False are equal, giving 2.
  3. Text is different. The string "1" is never equal to the number 1, so it only matches itself.
  4. Rows are items. In the grid, [1, 0] matches two whole rows, but the bare number 1 matches nothing, because it is never a top-level item.

Frequently Asked Questions

It returns 0. Unlike index() and remove(), count() never raises an error for a missing value.
count() compares with ==, and in Python True == 1 and 1.0 == 1 are both True. Equal values are counted no matter their type.
No. It only compares the top-level items. Count inside the rows yourself, for example sum(row.count(1) for row in grid).
Yes, strings must match exactly. To ignore case, lowercase the items first: [w.lower() for w in words].count("yes").
Use collections.Counter(lst), which builds a mapping from each value to its count in one pass, or loop and update a dict.

Next: list.extend()

Add every item from an iterable to the end of the list, in place.

extend() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful