Counting each letter's frequency in a string

dictionary, python

Solution

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

Problem

This is a question from pyschools. I did get it right, but I'm guessing that there would be a simpler method. Is this the simplest way to do this? ``` def countLetters(word): letterdict={} for letter in word: letterdict[letter] = 0 for letter in word: letterdict[letter] += 1 return letterdict ``` This should look something like this: ``` >>> countLetters('google') {'e': 1, 'g': 2, 'l': 1, 'o': 2} ```

Original source