Saturday, January 11, 2025
Google search engine
HomeLanguagesPython program to generate a list of alphabets in lexical order

Python program to generate a list of alphabets in lexical order

Prerequisite: chr()

The following methods explain how a python list with alphabets in lexical(alphabetic) order can be generated dynamically using chr() method.

Approach:

The core of the concept in both methods is almost same, they only differ in implementation:

  1. Validate size of alphabet list(size=26).
  2. If size>26, change to 26.
  3. If sizeā‰¤0, no meaning left to the function and to make it meaningful set size to 26.
  4. Use chr() to produce the list using any of following methods.

Method 1: Using loop

Python3




# function to get the list of alphabets
def generateAlphabetListDynamically(size = 26):
Ā Ā size = 26 if (size > 26 or size <= 0) else size
Ā Ā Ā 
Ā Ā # Empty list
Ā Ā alphabetList = []
Ā Ā Ā 
Ā Ā # Looping from 0 to required size
Ā Ā for i in range(size):
Ā Ā Ā Ā alphabetList.append(chr(65 + i))
Ā Ā Ā 
Ā Ā # return the generated list
Ā Ā return alphabetList
Ā 
Ā 
alphabetList = generateAlphabetListDynamically()
print('The alphabets in the list are:', *alphabetList)


Output:

The alphabets in the list are: 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

Time Complexity: O(n), where n is length of alphabetList.
Auxiliary Space: O(n), where n is length of alphabetList.

Method 2: using list comprehension

Python3




def generateAlphabetListDynamically(size=26):
Ā Ā Ā Ā size = 26 if (size > 26 or size <= 0) else size
Ā 
Ā Ā Ā Ā # Here we are looping from 0 to upto specified size
Ā Ā Ā Ā # 65 is added because it is ascii equivalent of 'A'
Ā Ā Ā Ā alphabetList = [chr(i+65) for i in range(size)]
Ā 
Ā Ā Ā Ā # returning the list
Ā Ā Ā Ā return alphabetList
Ā 
Ā 
# Calling the function to get the alphabets
alphabetList = generateAlphabetListDynamically()
print('The alphabets in the list are:', *alphabetList)


Output:

The alphabets in the list are: 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

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach 3: Using the string library

This approach is simpler compared to the other two, as we donā€™t have to use any loop or list comprehension. Instead, we just use the string.ascii_uppercase constant, which returns a string containing all ASCII uppercase letters. Then, we just slice it to get the first size characters and convert it to a list.
Ā 

Python3




import string
Ā 
def generateAlphabetListDynamically(size=26):
Ā Ā size = 26 if (size > 26 or size <= 0) else size
Ā Ā alphabetList = list(string.ascii_uppercase[:size])
Ā Ā return alphabetList
alphabetList = generateAlphabetListDynamically()
print('The alphabets in the list are:', *alphabetList)


Output

The alphabets in the list are: 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

Time Complexity: O(n)
Auxiliary Space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?