Use len(mapping) to count the keys in a Python dictionary. Each key counts once, even when its value is empty. In {"first": [1, 2], "second": []} there are two keys. To count the entries inside the values, count those inner lists separately.
deliveries = {"Monday": ["A", "B"], "Tuesday": []}
assert len(deliveries) == 2
assert sum(len(ids) for ids in deliveries.values()) == 2
deliveries["Monday"] = ["C"]
assert len(deliveries) == 2
assert sum(len(ids) for ids in deliveries.values()) == 1
len(deliveries) counts two recorded days both times. The sum counts the delivery IDs under those days. It falls from two to one when you replace Monday’s list.
Count current keys, including empty values
A key with an empty string, zero, False, or None is still a key. A dictionary can therefore be nonempty while all its values are false in a condition. To find the number of explicitly non-null values, use a predicate such as sum(value is not None for value in mapping.values()). A truthiness test would drop zeros and empty strings too.
Repeated writes to an equal key update the existing entry. For example, {"red": 1, "red": 2} has length one and stores 2 for “red.” A count of dictionary keys can be much smaller than a count of source rows if you overwrite repeats when importing records. If duplicate identifiers matter, detect them before they are collapsed into the mapping.
Count the values inside a nested dictionary
For a dictionary of lists, sum(len(items) for items in mapping.values()) counts the slots in those lists. For a dictionary of dictionaries, the same expression counts the keys in each inner dictionary and adds the counts. It does not count only the distinct inner keys shared among those dictionaries.
Consider {"north": {"tulip": 3}, "south": {"tulip": 5}}. There are two sites, two site-and-flower entries, one distinct flower name, and eight flowers. Count outer keys for sites, sum the lengths of inner dictionaries for entries, collect inner keys in a set for distinct names, and sum the stored numbers for flowers.
Use a snapshot when the count must agree with the records
len(mapping) reads the dictionary as it is now. A view such as keys = mapping.keys() also reflects later changes to the underlying dictionary. If the terminology in your report calls something a snapshot, build a stable collection first and derive both the displayed count and rows from that same collection. For nested mutable values, a shallow copy of the mapping does not snapshot their contents.
In an ordinary short script, len(mapping) is direct and efficient. Creating list(mapping.keys()) just to count its entries copies the keys for no benefit. Use the list when you actually need an independent sequence of key names.
Check the difference between item count and storage size
The number of entries says nothing about the number of bytes taken by the dictionary or all the objects it references. A mapping with one large list can contain more data than a mapping with many small values. If you need to estimate memory for capacity planning, choose a measurement that matches the whole operation and state whether it includes referenced objects.
A good test for a dictionary count includes an empty record, a record with a null value, and a duplicate-key update. For a nested report, add a record with zero children. You can tell from those cases whether your calculation really counts groups, children, or nonempty groups.