Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Count Like a Pro: One-Line Python Magic with Counter

Data Science With Dennis 1 min read

Stop writing loops to tally items in a list. Python's Counter from the collections module lets you count elements in a single line. For example:

from collections import Counter
counts = Counter(my_list)

This returns a dictionary-like object where each key is an item and its value is the count. Perfect for frequency analysis, data cleaning, or any task requiring quick tallies. Use .most_common(n) to get the top n items. A simple yet powerful tool for any Python developer.

React