Open In App

Python – Ways to Initialize List with Alphabets

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

This article discusses various methods to initialize list with alphabets. Let’s explore them:

Using string module

The string module provides predefined constants for lowercase and uppercase alphabets.

Python
import string

a = list(string.ascii_lowercase)
b = list(string.ascii_uppercase)

print(a)
print(b)

Output:

[‘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’]

Explanation:

  • string.ascii_lowercase constant contains all lowercase alphabets from ‘a’ to ‘z’.
  • string.ascii_uppercase constant contains all uppercase alphabets from ‘A’ to ‘Z’.

Using List Comprehension with chr()

If we want to initialize a list with a custom range of alphabets, we can use list comprehension along with the chr() function. The chr() function converts an ASCII code to its corresponding character.

Python
a = [chr(i) for i in range(ord('a'), ord('z') + 1)]
b = [chr(i) for i in range(ord('A'), ord('Z') + 1)]

print(a)
print(b)

Output:

[‘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’]

Explanation:

  • ord(‘a’) function returns the ASCII value of ‘a’ (97), and we can use chr() to convert it back to a character.
  • By using list comprehension, we can generate any range of alphabets.

Using for Loop

A more manual approach involves using a for loop to append individual characters to the list. The loop iterates over the ASCII values for the characters and appends each character to the list.

Python
a = []
for i in range(ord('a'), ord('z') + 1):
    a.append(chr(i))

b = []
for i in range(ord('A'), ord('Z') + 1):
    b.append(chr(i))

print(a)
print(b)

Output:

[‘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’]

Explanation:

  • ord(‘a’) to ord(‘z’) gives ASCII values 97 to 122.
  • chr(i) converts each number back to a character.
  • Each character is added to the list using append() to form the alphabet list.
  • The same logic applies for ord(‘A’) to ord(‘Z’) to get uppercase letters.

Using map() Function with chr()

map() function can also be used to apply chr() to a range of ASCII values resulting in a list of alphabetic characters.

Python
a = list(map(chr, range(ord('a'), ord('z') + 1)))
b = list(map(chr, range(ord('A'), ord('Z') + 1)))

print(a)
print(b)

Output:

[‘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’]

Explanation:

  • map() function applies chr() to each number in the given range.
  • The list() function converts the result into a list.

Using itertools.chain()

itertools.chain() can be used to combine multiple sequences of characters into one list. This is especially useful when we want to initialize a list with both uppercase and lowercase alphabets in a single operation.

Python
import string, itertools

a = list(itertools.chain(string.ascii_lowercase, string.ascii_uppercase))

print(a)

Output:

[‘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’]

Explanation: itertools.chain() can concatenate multiple iterables which can be used to combine alphabets from different ranges.

For in-depth knowledge of the methods used in this article, read: map(), list(), chr(), chain(), Python, Python Strings.



Next Article
Practice Tags :

Similar Reads