Python | Ways to initialize list with alphabets
While working with lists, sometimes we wish to initialize the list with the English alphabets a-z. This is an essential utility in competitive programming and also in certain applications. Let’s discuss various approaches to achieve this.
Method #1 : Naive method
The most general method that comes in our mind is using the brute force method of running a loop till 26 and incrementing it while appending the letters in the list.
# Python3 code to demonstrate # Filling alphabets # using naive method # 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’]
Method #2 : Using list comprehension
This is method similar to the above method, but rather a shorthand to naive method as it uses the list comprehension technique to achieve the task.
# 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’]
Method #3 : Using map()
Using map() is the elegant way to perform this particular task. It type casts the numbers in a range to particular data type, char in this case and assigns to the list.
# Python3 code to demonstrate # Filling alphabets # using map() # 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’]
Method #4 : Using string.ascii_lowercase
The most pythonic and latest way to perform this particular task. Using this new inbuilt function will internally handle the coding part providing the useful shorthand for the user.
# Python3 code to demonstrate # Filling alphabets # using string import string # initializing empty list test_list = [] # printing initial list print ("Initial list : " + str(test_list)) # using string # for filling alphabets test_list = list(string.ascii_lowercase) # 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’]
Recommended Posts:
- Python | Initialize a dictionary with only keys from a list
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python | Ways to sum list of lists and return sum list
- Different ways to clear a list in Python
- Python | Ways to Spilt the list by some value
- Extending a list in Python (5 different ways)
- Python | Ways to flatten a 2D list
- Python | Ways to rotate a list
- Python | Ways to shuffle a list
- Python | Ways to remove duplicates from list
- Python | Ways to format elements of given list
- Python | Multiply all numbers in the list (3 different ways)
- Python | Ways to remove particular list element
- Python | Ways to print list without quotes
- Python | Ways to find length of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



