Is there a fast way to generate a dict of the alphabet in Python?
Question:
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.
Answers:
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}
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}
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})
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}
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}
If you plan to use it for counting, I suggest the following:
import collections
d = collections.defaultdict(int)
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}
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}
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}
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}
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')]
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'}
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
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.
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}
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}
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})
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}
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}
If you plan to use it for counting, I suggest the following:
import collections
d = collections.defaultdict(int)
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}
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}
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}
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}
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')]
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'}
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