import string
letter_count = dict(zip(string.ascii_lowercase, [0]*26))
Or maybe:
import string
import itertools
letter_count = dict(zip(string.lowercase, itertools.repeat(0)))
Can't decide which one I like more at the moment.
EDIT: I've decided that I like nosklo's the best :-)
import string
letter_count = dict.fromkeys(string.ascii_lowercase, 0)
I'll take a guess here: do you want to count occurences of letters in a text (or something similar)? There are better ways to do this than starting with an initialized dictionary.
For one, there is defaultdict
in the collections
module:
>>> import collections
>>> letter_count = collections.defaultdict(int)
>>> the_text = 'the quick brown fox jumps over the lazy dog'
>>> for letter in the_text: letter_count[letter] += 1
...
>>> letter_counts
defaultdict(<type 'int'>, {' ': 8, 'a': 1, 'c': 1, 'b': 1, ... 'z': 1})
Then there is this way:
>>> dict((c, s.count(c)) for c in string.ascii_lowercase)
{'a': 1, 'b': 1, 'c': 1, ... 'z': 1}
Horrible performance, but short and easy to read. Might be worth it, if performance doesn't matter.