Counting each letter’s frequency in a string

In 2.7+:

import collections
letters = collections.Counter('google')

Earlier (2.5+, that’s ancient by now):

import collections
letters = collections.defaultdict(int)
for letter in word:
    letters[letter] += 1

Leave a Comment