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
Python | Which is faster to initialize lists?
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. [GFGTABS]
2 min read
Python | Sort the list alphabetically in a dictionary
In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let's see how to sort the list alphabetically in a dictionary. Sort a List Alphabetically in PythonIn Python, Sorting a List Alphabetically is
3 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
How to Concatenate Two Lists Index Wise in Python
Concatenating two lists index-wise means combining corresponding elements from both lists into a single element, typically a string, at each index . For example, given two lists like a = ["gf", "i", "be"] and b = ["g", "s", "st"], the task is to concatenate each element from a with the corresponding
3 min read
Ways to increment a character in Python
In python there is no implicit concept of data types, though explicit conversion of data types is possible, but it not easy for us to instruct operator to work in a way and understand the data type of operand and manipulate according to that. For e.g Adding 1 to a character, if we require to increme
4 min read
How to add Elements to a List in Python
In Python, adding elements to a list is a common operation that can be done in several ways. One of the simplest methods is using the append() method. In this article we are going to explore different methods to add elements in the list. Below is a simple Python program to add elements to a list usi
3 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
Python | Ways to find indices of value in list
In Python, it is common to locate the index of a particular value in a list. The built-in index() method can find the first occurrence of a value. However, there are scenarios where multiple occurrences of the value exist and we need to retrieve all the indices. Python offers various methods to achi
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