Skip to content

How to Make a List of the Alphabet in Python

How to Make a List of the Alphabet in Python Cover Image

In this tutorial, you’ll learn how to use Python to make a list of the entire alphabet. This can be quite useful when you’re working on interview assignments or in programming competitions. You’ll learn how to use the string module in order to generate a list of either and both the entire lower and upper case of the ASCII alphabet. You’ll also learn some naive implementations that rely on the ord() and chr() functions.

Using the string Module to Make a Python List of the Alphabet

The simplest and, perhaps, most intuitive way to generate a list of all the characters in the alphabet is by using the string module. The string module is part of the standard Python library, meaning you don’t need to install anything. The easiest way to load a list of all the letters of the alphabet is to use the string.ascii_letters, string.ascii_lowercase, and string.ascii_uppercase instances.

As the names describe, these instances return the lower and upper cases alphabets, the lower case alphabet, and the upper case alphabet, respectively. The values are fixed and aren’t locale-dependent, meaning that they return the same values, regardless of the locale that you set.

Let’s take a look at how we can load the lower case alphabet in Python using the string module:

# Loading the lowercase alphabet to a list
import string
alphabet = list(string.ascii_lowercase)
print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Let’s break down how this works:

  1. We import the string module
  2. We then instantiate a new variable, alphabet, which uses the string.ascii_lowercase instance.
  3. This returns a single string containing all the letters of the alphabet
  4. We then pass this into the list() function, which converts each letter into a single string in the list

The table below shows the types of lists you can generate using the three methods:

InstanceReturned List
list(string.ascii_letters)['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
list(string.ascii_lowercase)['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
list(string.ascii_uppercase)['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Returning a list of the alphabet in Python

In the following sections, you’ll learn how to make use of the chr and ord functions to generate a list of the alphabet in Python.

Using Python chr and ord to Make a Python List of the Alphabet

In this section, you’ll learn how to make use of the chr and ord functions to generate a list of the alphabet. The chr function converts an integer value into its corresponding Unicode value. Similarly, the ord function converts a Unicode value into its integer representation.

Use a For Loop to Make a Python List of the Alphabet

We can use the chr() function to loop over the values from 97 through 122 in order to generate a list of the alphabet in lowercase. The lowercase letters from a through z are represented by integers of 97 to 122. We’ll instantiate an empty list and append each letter to it. Let’s see what this looks like:

# Generate a list of the alphabet in Python with a for loop
alphabet = []
for i in range(97, 123):
    alphabet.append(chr(i))

print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

It’s not always convenient to remember the value of 97 (or 122). Because of this, we can use the ord() function to determine the integer value of the letter 'a' and then iterate through 26 additional letters. Let’s see what this looks like:

# Generate a list of the alphabet in Python with a for loop
alphabet = []
start = ord('a')
for i in range(26):
    alphabet.append(chr(start + i))

print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

In the next section, you’ll learn how to convert these for loops in Python list comprehensions.

Use a List Comprehension to Make a Python List of the Alphabet

You can convert many Python for loops into list comprehensions, which are much more concise ways of generating lists. For simple for loops, this makes many for loops significantly more readable. List comprehensions save us the trouble of first instantiating an empty list and can be written on a single line of code. Let’s see what list comprehensions looks like:

Python List Comprehensions Syntax
An overview of Python list comprehensions

We can see that we evaluate an expression for each item in an iterable. In order to do this, we can iterate over the range object between 97 and 122 to generate a list of the alphabet. Let’s give this a shot!

# Generate a list of the alphabet in Python with a list comprehensions
alphabet = [chr(value) for value in range(97, 123)]
print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

While our for loop wasn’t very complicated, converting it into a list comprehension makes it significantly easier to read! We can also convert our more dynamic version into a list comprehension, as show below:

# Generate a list of the alphabet in Python with a list comprehension
alphabet = [chr(value) for value in range(ord('a'), ord('a') + 26)]
print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

In the final section, you’ll learn how to use the map() function to generate a list of the alphabet in Python.

Using Map to Make a Python List of the Alphabet

In this section, you’ll make use of the map() function in order to generate the alphabet. The map function applies a function to each item in an iterable. Because of this, we can map the chr function to each item in the range covering the letters of the alphabet. The benefit of this approach is increased readability by being able to indicate simply what action is being taken on each item in an iterable.

Let’s see what this code looks like:

# Generate a list of the alphabet in Python with map and chr
alphabet = list(map(chr, range(97, 123)))
print(alphabet)

# Returns: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Here, we use the map() function and pass in the chr function to be mapped to each item in the range() covering 97 through 123. Because the map() function returns a map object, we need to convert it to a list by using the list() function.

Conclusion

In this tutorial, you learned a number of ways to make a list of the alphabet in Python. You learned how to use instances from the string module, which covers lowercase and uppercase characters. You also learned how to make use of the chr() and ord() functions to convert between Unicode and integer values. You learned how to use these functions in conjunction with a for loop, a list comprehension, and the map() function.

To learn more about the string module, check out the official documentation here.

Additional Resources

To learn more about related topics, check out the articles listed below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *