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’]
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.