Python - Ways to Initialize List with Alphabets
Last Updated :
14 Apr, 2025
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.
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.
Similar Reads
How to Initialize a List in Python
Python List is an ordered collections on items, where we can insert, modify and delete the values. Let's first see how to initialize the list in Python with help of different examples. Initialize list using square brackets []Using [] we can initialize an empty list or list with some items. Python# I
2 min read
Ways to create a dictionary of Lists - Python
A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Initializing dictionary with Empty Lists in Python
In Python, it's common to encounter scenarios where you need to store lists in a dictionary. Often, this involves checking if a key exists and then creating a list for that key. However, a more efficient approach is to initialize the dictionary keys with empty lists from the start. Let's explore som
5 min read
Sort a list in Python without sort Function
Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sor
3 min read
How to add Elements to a List in Python
In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method:Pythona = [1, 2, 3] a.append(4) prin
2 min read
Alphabet range in Python
When working with strings and characters in Python, you may need to create a sequence of letters, such as the alphabet from 'a' to 'z' or 'A' to 'Z'. Python offers various options for accomplishing this, taking advantage of its rich string handling features. This article will go over numerous ways t
3 min read
Access List Items in Python
Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
range() to a list in Python
In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m
2 min read
How to iterate through list of tuples in Python
In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these
2 min read
Convert string to a list in Python
Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read