Python | Ways to initialize list with alphabets

While working with Python lists, sometimes we wish to initialize the list with the English alphabet a-z or A-Z. This is an essential utility in competitive programming and also in certain applications. Let’s discuss various methods to achieve this using Python.

Initialize the list with alphabets using string.ascii_uppercase

The most pythonic and latest way to perform this particular task. Using this new inbuilt function will internally handle the coding part providing a useful shorthand for the user. 

Note: You can use lowercase instead of uppercase to generate lower alphabets.

Python3




import string
 
# initializing empty list
test_list = []
 
# using string for filling alphabets
test_list = list(string.ascii_uppercase)
 
# printing resultant list
print ("List after insertion : " + str(test_list))


Output :

List after insertion : [β€˜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’]

Initialize the list with alphabets using a naive method

The most general method that comes to our mind is using the brute force method of running a Python loop till 26 and incrementing it while appending the letters in the list. Refer to ASCII Table for more.

Python3




# initializing empty list
test_list = []
 
# printing initial list
print ("Initial list : " + str(test_list))
 
# using naive method
# for filling alphabets
alpha = 'a'
for i in range(0, 26):
    test_list.append(alpha)
    alpha = chr(ord(alpha) + 1)
 
# printing resultant list
print ("List after insertion : " + str(test_list))


Output : 

Initial list : [] 

List after insertion : [β€˜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’]

Initialize the list with alphabets using list comprehension

This is a method similar to the above method, but rather a shorthand for the naive method as it uses the list comprehension technique to achieve the task. 

Python3




# Python3 code to demonstrate
# Filling alphabets
# using list comprehension
 
# initializing empty list
test_list = []
 
# printing initial list
print ("Initial list : " + str(test_list))
 
# using list comprehension
# for filling alphabets
test_list = [chr(x) for x in range(ord('a'), ord('z') + 1)]
 
# printing resultant list
print ("List after insertion : " + str(test_list))


Output :

Initial list : [] 

List after insertion : [β€˜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’]

Initialize the list with alphabets using a map()

Using a map() is an elegant way to perform this particular task. It type casts the numbers in a range to a particular data type, char in this case, and assigns them to the list. 

Python3




# initializing empty list
test_list = []
 
# printing initial list
print ("Initial list : " + str(test_list))
 
# using map()
# for filling alphabets
test_list = list(map(chr, range(97, 123)))
 
# printing resultant list
print ("List after insertion : " + str(test_list))


Output :

Initial list : [] 

List after insertion : [β€˜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’]



Contact Us