- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 certain applications.
Example
# 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)) # 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)) # 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)) # 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'] 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'] 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'] 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']
- Related Articles
- Python – Filter rows with only Alphabets from List of Lists
- Python - Ways to rotate a list
- 3 ways to initialize an object in Java
- Python - Ways to flatten a 2D list
- Python - Ways to merge strings into list
- Python - Ways to remove duplicates from list
- Different ways to clear a list in Python
- Python - Ways to create triplets from given list
- Python - Ways to format elements of given list
- Python - Ways to iterate tuple list of lists
- Java Program to Initialize a List
- How to initialize List in Kotlin?
- Initialize tuples with parameters in Python
- Python - Ways to find indices of value in list
- List comprehension and ord() in Python to remove all characters other than alphabets

Advertisements