Python Dictionary Count: How to Count Items With a Dict

To do a Python dictionary count, loop over your data with counts[item] = counts.get(item, 0) + 1, or pass the whole list to collections.Counter. Either way, each key holds how often it showed up.

In this tutorial, we’ll build a Python dictionary count three ways, then count the values already inside a dict. I tested every snippet on Python 3.12.5.

The sample coffee orders we’ll count in Python

Every example imports the same two objects from cart.py:

# Drink orders at a Denver coffee cart, Tuesday 11/03/2026
orders = ["Latte", "Mocha", "Latte", "Cold Brew", "Latte",
          "Chai", "Mocha", "Latte", "Cold Brew", "Drip"]

favorites = {"Emily Carter": "Latte", "James Rivera": "Mocha",
             "Olivia Brooks": "Latte", "Ryan Mitchell": "Chai",
             "Ava Thompson": "Latte", "Noah Bennett": "Cold Brew"}

orders has ten drinks (four lattes), and favorites maps six customers to a drink. You’ll start most counts from an empty Python dictionary.

Method 1: Count items with a dictionary and get() (Recommended)

You don’t need an import, and it works in any Python version:

from cart import orders, favorites

counts = {}
for drink in orders:
    counts[drink] = counts.get(drink, 0) + 1

print(counts)
print("Lattes sold:", counts["Latte"])
print("Distinct drinks:", len(counts))

Output:

{'Latte': 4, 'Mocha': 2, 'Cold Brew': 2, 'Chai': 1, 'Drip': 1}
Lattes sold: 4
Distinct drinks: 5

Latte comes out on top with 4, and five different drinks were sold.

VS Code output of a Python dictionary count built with get() over ten coffee orders
The get() loop turns ten orders into five drink totals, with Latte at 4.

How does this code work?

counts.get(drink, 0) returns the current total, or 0 for a drink we haven’t seen yet. Adding 1 and storing it back is the whole trick.

len(counts) then gives the number of distinct drinks. For bumping a single key, see incrementing a value in a Python dictionary.

Method 2: Count items with collections.Counter

Counter is a dictionary subclass built for counting. Hand it the same orders list:

from cart import orders, favorites

from collections import Counter

counts = Counter(orders)
print(counts)
print("Top 2:", counts.most_common(2))
print("Total orders:", counts.total())

Output:

Counter({'Latte': 4, 'Mocha': 2, 'Cold Brew': 2, 'Chai': 1, 'Drip': 1})
Top 2: [('Latte', 4), ('Mocha', 2)]
Total orders: 10

Same five totals, the top two drinks, and 10 orders overall.

VS Code output of collections.Counter counting coffee orders with most_common() and total()
most_common(2) ranks the drinks, and total() adds every count.

How does this code work?

Counter(orders) runs the counting loop in C. most_common(2) sorts by count for you, and total() adds every count (it needs Python 3.10+).

Subtraction and other tricks are in the Counter function in Python. To rank a plain dict, see sorting a Python dictionary.

Method 3: Count with defaultdict(int) and skip the KeyError

On a plain dict, counts[drink] += 1 fails on the first new drink. A defaultdict fills in 0 instead. Same orders:

from cart import orders, favorites

from collections import defaultdict

plain = {}
try:
    for drink in orders:
        plain[drink] += 1
except KeyError as err:
    print("Plain dict -> KeyError:", err)

counts = defaultdict(int)
for drink in orders:
    counts[drink] += 1
print(dict(counts))

Output:

Plain dict -> KeyError: 'Latte'
{'Latte': 4, 'Mocha': 2, 'Cold Brew': 2, 'Chai': 1, 'Drip': 1}

The plain dict stops at KeyError: 'Latte'; the defaultdict finishes with the same five totals.

VS Code output of a KeyError from counts[drink] += 1 on a plain dict and the defaultdict(int) fix
+= 1 on a missing key fails; defaultdict(int) starts it at 0.

How does this code work?

+= 1 reads the old value first, and a new key has none; int() supplies 0. The Python dictionary KeyError guide covers other causes.

Important: merely reading counts["Tea"] inserts Tea with 0. With list instead of int, it builds a Python dictionary of lists.

How do you count values inside a dictionary?

Sometimes the dictionary already exists. I’ll switch to favorites:

from cart import orders, favorites

from collections import Counter

print("Customers:", len(favorites))
print("Latte fans:", list(favorites.values()).count("Latte"))
print(Counter(favorites.values()))

others = sum(1 for d in favorites.values() if d != "Latte")
print("Other drinks:", others)

Output:

Customers: 6
Latte fans: 3
Counter({'Latte': 3, 'Mocha': 1, 'Chai': 1, 'Cold Brew': 1})
Other drinks: 3

Six customers: three latte fans and three others.

VS Code output of Python counting values in a dictionary with len(), list.count(), Counter and sum()
Counter(favorites.values()) tallies every value; the generator counts only matches.

How does this code work?

len(favorites) counts key-value pairs; counting the keys in a Python dictionary goes deeper.

.values() yields every drink, so count() finds one and Counter tallies all. See iterating through a Python dictionary.

Which Python dictionary count method should you use?

MethodUse it whenWatch out for
get() loopNo imports, full controlSlightly more code
CounterCounting a whole list, top-Ntotal() needs 3.10+
defaultdict(int)Counting inside a bigger loopLookups insert missing keys

I’d use Counter for one-off counts and get() when you’re mixing the count with other logic.

Things to keep in mind when counting with a dictionary

  • Keys are case-sensitive. “Latte” and “latte” are two keys; you’ll want .lower().
  • Lists can’t be keys. Use tuples instead.
  • Just counting a list? See counting occurrences in a Python list.

Got other things to tally? Pick up from here:

Frequently asked questions

How do you count items using a dictionary in Python?

Use counts[x] = counts.get(x, 0) + 1 in a loop, or Counter(data).

How do I count how many times a value appears in a dictionary?

Use list(d.values()).count(value) for one value, or Counter(d.values()) for all of them.

How do I count the number of items in a dictionary?

len(d) returns the number of key-value pairs.

Is Counter faster than a dictionary loop?

Usually, for a whole list, because Counter counts in C.

How do I get the most common item from a count dictionary?

Use Counter.most_common(1), or max(counts, key=counts.get) on a plain dict.

Where is collections.Counter documented?

See collections.Counter in the official Python docs.