Keeping tallies in Python

Posted in python, collections on July 25, 2012 by Ali

Python’s collections module has some of the most consistently useful collection data structures you will need for everyday programming. Here’s one I didn’t know about, collections.Counter (Python 2.7 only!). It is designed to keep “tallies” or count instances of something. The example will make it all clear:

from collections import Counter
cars = Counter()
# I see one go past, it is red
cars['red'] += 1
# And a green one
cars['blue'] += 1
# etc

This is pretty much like a defaultdict with an integer value, but it is convenient and neat, with a useful constructor.

There’s more. “Are two strings anagrams?”

are_anagrams = lambda s1, s2: Counter(s1) == Counter(s2)

Replied the smartass.