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)

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next
Similar Reads
Complete Tutorials