collections.Counter: Counting values in a Python sequence

collections.Counter: Counting values in a Python sequence

Want to count values in a Python sequence? Use Counter:

c = Counter('aaabbca')
c  # Counter({'a': 4, 'b': 2, 'c': 1})
c.most_common()  # [('a', 4), ('b', 2), ('c', 1)]
c.most_common(2) # [('a', 4), ('b', 2)]

Bonus: Counter inherits from dict, getting its methods + operators.