Skip to main content

How to Count How Many Times an Item Appears in a List in Python with count()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A week of weather, a pile of votes, a list of answers: often you want to know how many times one value is in a list. count() goes through the list and tells you. This guide counts the sunny days of a week on Python 3.15.2.

Syntax​

Syntax
<number> = <list>.count(<item>)
PartWhat it means
<list>The list to look through
<item>The value to count
<number>How many items of the list are equal to <item>, as a whole number. It's 0 when there are none

The Python tutorial describes list.count in one line: "Return the number of times value appears in the list."

The Example​

A list holds the weather of seven days: sun, rain or cloud. The script counts the sunny days, the rainy days, and the days with snow, which the list doesn't have.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

weather.py
weather = ["sun", "rain", "sun", "cloud", "sun", "rain", "sun"]

print(weather.count("sun"))
print(weather.count("rain"))
print(weather.count("snow"))

The weather.py script in a macOS-style code window: a weather list of seven days with sun, rain and cloud, then prints of weather.count for sun, rain and snow.

Step 2: Run It​

Terminal
uv run python weather.py
Output
4
2
0

The terminal after uv run python weather.py: 4, then 2, then 0.

sun is in the list four times and rain twice. snow isn't in the list at all, and count() gave 0 for it: a missing item is no error. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Counting with a different capital letterweather.count("Sun") gave 0, because "Sun" and "sun" are different stringsMake the letters the same first. For the list ["Sun", "sun", "SUN"], a copy with every item in lowercase counted 3 for "sun"
Counting a part of an itemweather.count("su") gave 0. A list counts whole items only. A string is different: "sun rain sun".count("su") gave 2Count the whole item: weather.count("sun")
Calling it with no itemweather.count() raised TypeError: list.count() takes exactly one argument (0 given)Pass the one value to count

FAQ​

How do I count every item at once?​

Use Counter from the collections module. After from collections import Counter, the call Counter(weather) gave Counter({'sun': 4, 'rain': 2, 'cloud': 1}), and Counter(weather).most_common(1) gave [('sun', 4)], the item that appears most with its count.

How is count() different from len()?​

len() counts all the items, and count() only the ones equal to the value you pass. len(weather) gave 7. The three counts for sun, rain and cloud added up to the same 7.

How does count() decide that two items are the same?​

It counts the items that are equal to the value. Python treats 1, 1.0 and True as equal: 1 == 1.0 and 1 == True both gave True. So [1, 1.0, True, 2].count(1) gave 3.

Does count() work on a tuple?​

Yes. ("sun", "rain", "sun").count("sun") gave 2.

How do I check whether an item is in the list at all?​

Use in, which is plainer than comparing a count with zero. "snow" in weather gave False. To ask whether an item is there more than once, compare the count: weather.count("rain") > 1 gave True.

Conclusion​

weather.count("sun") went through the seven days and gave 4. count() returns how many items are equal to the value, and 0 when the value isn't in the list.

Additional Resources​