views:

734

answers:

7

I want to generate a dict with the letters of the alphabet as the keys, something like

letter_count = {'a': 0, 'b': 0, 'c': 0}

what would be a fast way of generating that dict, rather than me having to type it in?

Thanks for your help.

EDIT
Thanks everyone for your solutions :)

nosklo's solution is probably the shortest

Also, thanks for reminding me about the Python string module.

+3  A: 

There's this too:

import string
letter_count = dict((letter, 0) for letter in string.ascii_lowercase)
recursive
Use `string.ascii_lowercase`. What if you've missed (typed twice) a letter? How do you tell it now, in 6 month? `string.ascii_lowercase` doesn't have such problems.
J.F. Sebastian
Thanks. I was looking for that, but couldn't find it under string.lowercase where i was looking.
recursive
+8  A: 

Here's a compact version, using a list comprehension:

>>> import string
>>> letter_count = dict( (key, 0) for key in string.ascii_lowercase )
>>> letter_count
{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0,
 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 
't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
Federico Ramponi
credits to other answers for ascii_lowercase :D
Federico Ramponi
You've got an extra pair of parens there: the parens in dict() are enough for the parser to understand the generator expression.
Jouni K. Seppänen
+11  A: 
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.

hop
string.lowercase depends on locale. See my answer.
J.F. Sebastian
yes, of course. might be what i want, though (works with the second example)
hop
+2  A: 
import string
letters = string.ascii_lowercase
d = dict(zip(letters, [0]*len(letters))
J.F. Sebastian
A: 

Yet another 1-liner Python hack:

letter_count = dict([(chr(i),0) for i in range(97,123)])
Jeff Bauer
Definitely a hack. At the very least, use range(ord('a'), ord('z')+1). That at least describes the intent to a reader.
Tom
Oh, give me break. It was *described* as a 1-liner hack.
Jeff Bauer
+8  A: 

If you plan to use it for counting, I suggest the following:

import collections
d = collections.defaultdict(int)
Kamil Kisiel
+22  A: 

I find this solution more elegant:

import string
d = dict.fromkeys(string.ascii_lowercase, 0)
nosklo
duh! one should re-read the docs for the trivial stuff from time to time ;)
hop
dict.fromkeys() is very useful, you just have to be aware that using a mutable object (such as a list) for the value will store *references* to that object in all the dictionary values.
Alec Thomas