Is there a fast way to generate a dict of the alphabet in Python?
Asked Answered
E

14

55

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?

Embryologist answered 17/1, 2009 at 16:51 Comment(1)
for what will you use the dict later? maybe there's a more elegant solution in the first place.Machiavellian
E
92

I find this solution 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}
Elaina answered 17/1, 2009 at 18:36 Comment(8)
duh! one should re-read the docs for the trivial stuff from time to time ;)Machiavellian
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.Entrails
Been scripting python for years, and somehow I didn't know that... thanksCila
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") ???Schinica
@Schinica 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.Elaina
@Elaina Can you please tell me how would you go about incrementing the int from 0 through 25?Coniferous
@Coniferous {ch: n for n, ch in enumerate(string.ascii_lowercase)}Elaina
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)}Intolerable
M
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})
Machiavellian answered 17/1, 2009 at 17:3 Comment(2)
string.lowercase depends on locale. See my answer.Summitry
yes, of course. might be what i want, though (works with the second example)Machiavellian
C
10

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}
Choragus answered 22/7, 2018 at 14:39 Comment(0)
H
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}
Haynes answered 17/1, 2009 at 17:1 Comment(1)
You've got an extra pair of parens there: the parens in dict() are enough for the parser to understand the generator expression.Percentile
T
8

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

import collections
d = collections.defaultdict(int)
Teishateixeira answered 17/1, 2009 at 18:19 Comment(0)
L
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}
Lucky answered 17/1, 2009 at 18:17 Comment(3)
Definitely a hack. At the very least, use range(ord('a'), ord('z')+1). That at least describes the intent to a reader.Desireedesiri
Oh, give me break. It was described as a 1-liner hack.Lucky
d = {chr(x): 0 for x in range(ord('a'), ord('z')+1)} This is more conciseIntolerable
P
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}
Playgoer answered 17/1, 2009 at 16:59 Comment(2)
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.Summitry
Thanks. I was looking for that, but couldn't find it under string.lowercase where i was looking.Playgoer
S
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}
Summitry answered 17/1, 2009 at 17:5 Comment(0)
L
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}
Latency answered 21/6, 2019 at 8:18 Comment(0)
A
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'}
Analisaanalise answered 4/3, 2021 at 16:33 Comment(0)
I
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}
Ichnography answered 29/1, 2020 at 13:24 Comment(0)
D
0

If you are looking for a multilingual solution for alphabets other than English, I recommend the Alphabetic library, which can be installed via: pip install alphabetic

After installing, import the library, create a WritingSystem instance and select the alphabet of the desired language (Alphabetic covers a wide range of languages):

from alphabetic import WritingSystem

ws = WritingSystem()

# English
print(dict.fromkeys(ws.by_language(ws.Language.English, as_list=True), 0))
# {'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, '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}

# Hebrew
print(dict.fromkeys(ws.by_language(ws.Language.Hebrew, as_list=True), 0))
# {'א': 0, 'ב': 0, 'ג': 0, 'ד': 0, 'ה': 0, 'ו': 0, 'ז': 0, 'ח': 0, 'ט': 0, 'י': 0, 'כ': 0, 'ך': 0, 'ל': 0, 'מ': 0, 'ם': 0, 'נ': 0, 'ן': 0, 'ס': 0, 'ע': 0, 'פ': 0, 'ף': 0, 'צ': 0, 'ץ': 0, 'ק': 0, 'ר': 0, 'ש': 0, 'ת': 0}

# Georgian
print(dict.fromkeys(ws.by_language(ws.Language.Georgian, as_list=True), 0))
# {'ა': 0, 'ბ': 0, 'გ': 0, 'დ': 0, 'ე': 0, 'ვ': 0, 'ზ': 0, 'თ': 0, 'ი': 0, 'კ': 0, 'ლ': 0, 'მ': 0, 'ნ': 0, 'ო': 0, 'პ': 0, 'ჟ': 0, 'რ': 0, 'ს': 0, 'ტ': 0, 'უ': 0, 'ფ': 0, 'ქ': 0, 'ღ': 0, 'ყ': 0, 'შ': 0, 'ჩ': 0, 'ც': 0, 'ძ': 0, 'წ': 0, 'ჭ': 0, 'ხ': 0, 'ჯ': 0, 'ჰ': 0, 'ჱ': 0, 'ჲ': 0, 'ჳ': 0, 'ჴ': 0, 'ჵ': 0}

# Korean
print(dict.fromkeys(ws.by_language(ws.Language.Korean, as_list=True), 0))
# {'ㄱ': 0, 'ㄲ': 0, 'ㄴ': 0, 'ㄷ': 0, 'ㄸ': 0, 'ㄹ': 0, 'ㅁ': 0, 'ㅂ': 0, 'ㅃ': 0, 'ㅅ': 0, 'ㅆ': 0, 'ㅇ': 0, 'ㅈ': 0, 'ㅉ': 0, 'ㅊ': 0, 'ㅋ': 0, 'ㅌ': 0, 'ㅍ': 0, 'ㅎ': 0, 'ㅏ': 0, 'ㅐ': 0, 'ㅑ': 0, 'ㅒ': 0, 'ㅓ': 0, 'ㅔ': 0, 'ㅕ': 0, 'ㅖ': 0, 'ㅗ': 0, 'ㅘ': 0, 'ㅙ': 0, 'ㅚ': 0, 'ㅛ': 0, 'ㅜ': 0, 'ㅝ': 0, 'ㅞ': 0, 'ㅟ': 0, 'ㅠ': 0, 'ㅡ': 0, 'ㅢ': 0, 'ㅣ': 0}
Duotone answered 11/6 at 23:8 Comment(0)
C
-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')]
Credenza answered 13/4, 2020 at 20:29 Comment(1)
While this code may solve the question, including an explanation 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.Killam
S
-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

Supat answered 10/2, 2022 at 17:0 Comment(1)
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.Misdeed

© 2022 - 2024 — McMap. All rights reserved.