Counting how many times each item appears in a list is a problem you'll run into constantly — tallying votes, counting word occurrences, grouping survey results. This post shows how to do it manually with a dictionary, then with Python's built-in shortcut.

The manual approach walks the list once, looping through a list and using a dictionary to keep a running count for each distinct value.

Python Program to Count Frequency Using a Dictionary

items = ["apple", "banana", "apple", "orange", "banana", "apple"]

frequency = {}
for item in items:
    frequency[item] = frequency.get(item, 0) + 1

for item, count in frequency.items():
    print(f"{item}: {count}")

Output:

apple: 3
banana: 2
orange: 1

dict.get(item, 0) is the trick here — it returns the existing count for item, or 0 if it hasn't been seen yet, so you can always add 1 without a separate "have I seen this before?" check.


Python Program to Count Frequency Using Counter

The collections module ships a purpose-built class for exactly this job: Counter.

from collections import Counter

items = ["apple", "banana", "apple", "orange", "banana", "apple"]

frequency = Counter(items)
print(frequency)
print(frequency.most_common(1))

Output:

Counter({'apple': 3, 'banana': 2, 'orange': 1})
[('apple', 3)]

Counter behaves like a dictionary but adds handy extras, like most_common(n) for instantly getting the top n most frequent items — useful well beyond this toy example, e.g. finding the most common word in a large text file.