For a more complex example, we use the Counter with range loops. We add to the counter based on ranges of integers. The counts accumulate.
from collections import Counter
# Create new counter.
freqs = Counter()
# Add to the counter based on ranges of numbers.
for i in range(0, 10):
freqs[i] += 1
for i in range(0, 5):
freqs[i] += 1
for i in range(0, 2):
freqs[i] += 1
# Display counter contents.
for i in range(0, 10):
print(f
"Value at {i} is {freqs[i]}")
# The counter can be displayed directly.
print(freqs)
Value at 0 is 3
Value at 1 is 3
Value at 2 is 2
Value at 3 is 2
Value at 4 is 2
Value at 5 is 1
Value at 6 is 1
Value at 7 is 1
Value at 8 is 1
Value at 9 is 1
Counter({0: 3, 1: 3, 2: 2, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})