0Pricing
Python Academy · Lesson

Counter

Count items easily.

Counter is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Counter?

Counter is a dictionary subclass from the collections module built for counting. You give it items and it tallies how many times each appears.

Import it with from collections import Counter.

from collections import Counter
c = Counter(['a', 'b', 'a', 'c', 'a'])
print(c)

Counting characters

Pass any iterable, including a string, and Counter counts each element. Strings are counted character by character.

from collections import Counter
c = Counter('mississippi')
print(c)

Looking up a count

Access a count like a dictionary key. A missing key returns 0 instead of raising KeyError — a handy difference from a normal dict.

from collections import Counter
c = Counter('banana')
print(c['a'])
print(c['z'])

Most common items

.most_common(n) returns the top n items as (item, count) pairs, sorted from most to least frequent.

from collections import Counter
words = ['red', 'blue', 'red', 'green', 'blue', 'red']
c = Counter(words)
print(c.most_common(2))

All items by frequency

Call .most_common() with no argument to get every item ordered by frequency.

from collections import Counter
c = Counter('aabbbcccc')
print(c.most_common())

Updating counts

The .update() method adds more counts from another iterable, rather than replacing them like a regular dict would.

from collections import Counter
c = Counter(['a', 'b'])
c.update(['a', 'a', 'c'])
print(c)

Counting from a dictionary

You can also build a Counter directly from a mapping of item to count, or with keyword arguments.

from collections import Counter
c = Counter(apples=3, pears=2)
print(c)
print(c['apples'])

Adding two Counters

The + operator combines two Counters by summing matching keys. Items with a zero or negative total are dropped.

from collections import Counter
a = Counter(x=2, y=1)
b = Counter(x=1, z=4)
print(a + b)

Subtracting counts

The - operator subtracts counts and keeps only positive results. Use .subtract() if you want to allow negatives.

from collections import Counter
a = Counter(x=5, y=2)
b = Counter(x=3, y=4)
print(a - b)

Total of all counts

Sum every count with sum(c.values()). This tells you how many items were counted in total.

from collections import Counter
c = Counter('mississippi')
print('Total letters:', sum(c.values()))

Listing elements

.elements() returns an iterator that repeats each element by its count. Wrap it in list() to see them all.

from collections import Counter
c = Counter(a=2, b=1, c=3)
print(sorted(c.elements()))

Quick Check

Given c = Counter('hello'), what does c['x'] return?

Recap: Counter

You learned that Counter:

  • Counts items from any iterable, including strings.
  • Returns 0 for missing keys instead of erroring.
  • Offers .most_common(n) to rank items by frequency.
  • Supports +, -, .update(), and .elements() for tallying work.
from collections import Counter
votes = ['yes', 'no', 'yes', 'yes', 'no']
print(Counter(votes).most_common(1))

Frequently asked questions

Is the “Counter” lesson free?

Yes — the full text of “Counter” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Counter”?

Count items easily. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Counter” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Counter
  2. defaultdict
  3. deque
  4. namedtuple and OrderedDict
← Back to Python Academy