51

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.

Community
  • 1
  • 1
Nope
  • 34,682
  • 42
  • 94
  • 119
  • for what will you use the dict later? maybe there's a more elegant solution in the first place. –  Jan 17 '09 at 17:20

13 Answers13

86

I find this solution more elegant:

import string
d = dict.fromkeys(string.ascii_lowercase, 0)
print(d)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • duh! one should re-read the docs for the trivial stuff from time to time ;) –  Jan 18 '09 at 01:22
  • 2
    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 Jan 19 '09 at 05:11
  • 1
    Been scripting python for years, and somehow I didn't know that... thanks – Ubiquitous Aug 15 '11 at 22:16
  • Excellent....but I am wondering why my dictionary keys are not in alphabetic order. d.keys() gives me ['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X', 'Z'] (which is not in alphabetic order) while string.ascii_uppercase is in alphabetic order ("ABCDEFGHIJKLMNOPQRSTUVWXYZ") ??? – d.putto Feb 20 '12 at 11:21
  • 4
    @d.putto because dicts don't have any order, so the order you get the keys back is not something you can rely upon. That's normal with dicts. – nosklo Feb 21 '12 at 17:43
  • @nosklo Can you please tell me how would you go about incrementing the int from 0 through 25? – devtye Jun 21 '16 at 18:52
  • 3
    @devtye `{ch: n for n, ch in enumerate(string.ascii_lowercase)}` – nosklo Jun 20 '18 at 18:54
  • 2
    If you do not want to remember library names and do not wish to use explicit import statements, there is a better way to do it: ```d = {chr(x): 0 for x in range(ord('a'), ord('z')+1)}``` – Lavanya Feb 05 '21 at 06:51
16
import string
letter_count = dict(zip(string.ascii_lowercase, [0]*26))

print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}

or maybe:

import string
import itertools
letter_count = dict(zip(string.ascii_lowercase, itertools.repeat(0)))

print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}

or even:

import string
letter_count = dict.fromkeys(string.ascii_lowercase, 0)

print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}

The preferred solution might be a different one, depending on the actual values you want in the dict.


I'll take a guess here: do you want to count occurences of letters in a text (or something similar)? There is a better way to do this than starting with an initialized dictionary.

Use Counter from the collections module:

import collections
the_text = 'the quick brown fox jumps over the lazy dog'
letter_counts = collections.Counter(the_text)

print(letter_counts)
# Counter({' ': 8, 'o': 4, 'e': 3, ... 'n': 1, 'x': 1, 'k': 1, 'b': 1})
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
9

This is very easy with dictionary comprehensions:

# Generate lowercase Mapping
{chr(i+96):i for i in range(1,27)}
# Generates :
# {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}

# Generate UPPERCASE Mapping
{chr(i+64):i for i in range(1,27)}
# Generates :
# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
user10084443
  • 151
  • 2
  • 1
8

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}
tzot
  • 92,761
  • 29
  • 141
  • 204
Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
8

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

import collections
d = collections.defaultdict(int)
Kamil Kisiel
  • 19,723
  • 11
  • 46
  • 56
5

Yet another 1-liner Python hack:

letter_count = dict([(chr(i),0) for i in range(97,123)])

print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
3

There's this too:

import string
letter_count = dict((letter, 0) for letter in string.ascii_lowercase)

print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
recursive
  • 83,943
  • 34
  • 151
  • 241
  • 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. – jfs Jan 17 '09 at 17:12
  • Thanks. I was looking for that, but couldn't find it under string.lowercase where i was looking. – recursive Jan 18 '09 at 23:44
2
import string

letters = string.ascii_lowercase
d = dict(zip(letters, [0]*len(letters)))
print(d)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

You can use dictionary and range directly, so you can create your own function and easily customize it.

def gen_alphabet(start, value):
    return {chr(ord('a') + i) : 0 for i in range(value)}

print(gen_alphabet('a', 26))

OUTPUT:

>>> {'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}
Kevin Sabbe
  • 1,412
  • 16
  • 24
1
import string

s0 = string.ascii_lowercase 
s1 = string.ascii_uppercase
s2 = string.ascii_letters
print(dict(enumerate(s0,1)))  # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
print(dict(enumerate(s1,1)))  # {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z'}
print(dict(enumerate(s2,1)))  # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z', 27: 'A', 28: 'B', 29: 'C', 30: 'D', 31: 'E', 32: 'F', 33: 'G', 34: 'H', 35: 'I', 36: 'J', 37: 'K', 38: 'L', 39: 'M', 40: 'N', 41: 'O', 42: 'P', 43: 'Q', 44: 'R', 45: 'S', 46: 'T', 47: 'U', 48: 'V', 49: 'W', 50: 'X', 51: 'Y', 52: 'Z'}
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
0

My variant :

Note : range A-Z in unicode => 65-90 (decimal)

d = dict.fromkeys([chr(j) for j in range(65, 90)], 0)
print(d)

OUTPUT :

>>> {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0}
Cortek29
  • 21
  • 1
  • 3
-1
a_to_z = [(chr(i+64), chr(i+64)) for i in range(1, 27)]

Output

[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('E', 'E'), ('F', 'F'), ('G', 'G'), ('H', 'H'), ('I', 'I'), ('J', 'J'), ('K', 'K'), ('L', 'L'), ('M', 'M'), ('N', 'N'), ('O', 'O'), ('P', 'P'), ('Q', 'Q'), ('R', 'R'), ('S', 'S'), ('T', 'T'), ('U', 'U'), ('V', 'V'), ('W', 'W'), ('X', 'X'), ('Y', 'Y'), ('Z', 'Z')]
M--
  • 25,431
  • 8
  • 61
  • 93
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 14 '20 at 00:34
-1

Construct a dict of words from alphabetical order A to zop each words adjectives, adverbs as to be included. Convert the list of adjective and adverbs from dict to list

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 11 '22 at 06:33