# Pure Python Histograms

Copied!
Happy Pythoning!

When you’re preparing to plot a histogram, it’s simplest to not think in terms of bins but rather to report how many times each value appears (a frequency table). A Python dictionary is well-suited for this task:

Python
``````>>> # Need not be sorted, necessarily
>>> a = (0, 1, 1, 1, 2, 3, 7, 7, 23)

>>> def count_elements(seq) -> dict:
...     """Tally elements from `seq`."""
...     hist = {}
...     for i in seq:
...         hist[i] = hist.get(i, 0) + 1
...     return hist

>>> counted = count_elements(a)
>>> counted
{0: 1, 1: 3, 2: 1, 3: 1, 7: 2, 23: 1}
``````
Copied!

`count_elements()` returns a dictionary with unique elements from the sequence as keys and their frequencies (counts) as values. Within the loop over `seq`, `hist[i] = hist.get(i, 0) + 1` says, “For each element of the sequence, increment its corresponding value in `hist` by 1.”

In fact, this is precisely what is done by the `collections.Counter` class from Python’s standard library, which subclasses a Python dictionary and overrides its `.update()` method:

Python
``````>>> from collections import Counter

>>> recounted = Counter(a)
>>> recounted
Counter({0: 1, 1: 3, 3: 1, 2: 1, 7: 2, 23: 1})
``````
Copied!

You can confirm that your handmade function does virtually the same thing as `collections.Counter` by testing for equality between the two:

Python
``````>>> recounted.items() == counted.items()
True
``````
Copied!

chrismarkella

Great presentation. Do you have any tutorial about the arrow syntax with the return type? I tried it with a dummy function but it didn’t force the type.

``````>>> def adding(a, b) -> int:
...   return a + b
...
7.3
``````