Count the Number of Times an Item Appears in a Python List

Here are 3 ways to count the number of times an item appears in a Python list:

(1) Using count() to count a specific item. For example, let’s count the number of times “mm” appears in a list:

my_list = ["mm", "bb", "cc", "mm", "mm", "cc"]

count_item = my_list.count("mm")

print(count_item)

The result is 3 times:

3

(2) Using Counter to count a specific item. Let’s again count the number of times “mm” appears in a list:

from collections import Counter

my_list = ["mm", "bb", "cc", "mm", "mm", "cc"]

count_all = Counter(my_list)
count_item = count_all.get("mm")

print(count_item)

The result is the same:

3

(3) Using Counter to count the number of times each item appears in a list:

from collections import Counter

my_list = ["mm", "bb", "cc", "mm", "mm", "cc"]

count_all = Counter(my_list)

print(count_all)

Here you’ll get the number of times that each item appears in a list:

Counter({'mm': 3, 'cc': 2, 'bb': 1})