Python - Count elements in list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To count elements in a Python list, use len(lst) for total count, lst.count(x) for occurrences of a specific element, or collections.Counter(lst) for a frequency dictionary of all elements. Counter is the most versatile — it gives you the count of every unique element in a single pass, supports arithmetic operations, and provides methods like most_common(). For pandas DataFrames, use value_counts().
Total Count with len()
Count Specific Element with list.count()
list.count() scans the entire list each time — O(n) per call. If you need counts for multiple elements, use Counter instead.
collections.Counter (Best for Frequency Counting)
Counter Arithmetic
Dictionary Comprehension Approach
These are equivalent to Counter but require more code. Use Counter unless you need custom logic during counting.
Counting with Conditions
Counting in Strings
Common Pitfalls
- Using
list.count()in a loop for all elements:for x in items: counts[x] = items.count(x)is O(n^2) becausecount()scans the full list for each element. UseCounter(items)for O(n) frequency counting of all elements in a single pass. - Expecting
Counterto raise KeyError for missing keys:Counterreturns 0 for missing keys, not aKeyError. This is convenient but can mask typos:counts["aplpe"]silently returns 0 instead of signaling an error. Verify key names if counts seem unexpectedly zero. - Confusing
len()withcount():len(lst)returns the total number of elements.lst.count(x)returns how many timesxappears. Using the wrong one gives meaningless results —len()for "how many items total,"count()for "how many of this specific item." - Modifying a list while counting: Inserting or removing elements during iteration changes the counts. Build the counter from the original list first, then modify.
Countertakes a snapshot — subsequent list changes do not update the counter. - Using Counter on unhashable elements:
Counterrequires hashable elements (strings, numbers, tuples). Passing a list of lists raisesTypeError: unhashable type: 'list'. Convert inner lists to tuples first:Counter(tuple(x) for x in nested_list).
Summary
- Use
len(lst)for total element count,lst.count(x)for a single element's frequency - Use
collections.Counterfor efficient frequency counting of all elements in one pass Counter.most_common(n)returns the n most frequent elements as a sorted list- Use
sum(1 for x in lst if condition)to count elements matching a predicate - Avoid
list.count()inside loops — it causes O(n^2) performance; preferCounter
Related reading
- Python - Exit Kafka queue once all messages have been read
- Python - Is a dictionary slow to find frequency of each character?
- Python - Tree traversal question
- Python 3 turn range to a list
- Python - Extract a PDF page as a jpeg
- Python - Extracting and Saving Video Frames
- Python add item to the tuple
- Python Convert complex dictionary of strings from Unicode to ASCII

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.