Different ways to initialize list with alphabets in python


When we are dealing with speech-based operations or creating a letter analysis table, we need a series of ordered alphabets. We can use python programming to perform such operations. Python offers numerous libraries and methods to create an ordered sequence of alphabets. We can contain these alphabetic sequences with the help of several collection datatypes such as lists, tuples dictionaries etc.

In this article, we will be performing a similar operation to print a list of alphabets initialized in the correct order. The produced list will contain all the 26 English language alphabets. Before we dive deep into the discussion, let’s quickly understand the overview of this article.

What is a list of alphabets?

A list of ordered alphabets is the sequences of 26 English language letters in the correct order. For example −

['A', 'B', 'C', 'D', 'E'....... 'Z']

Understanding the ascii scheme

When we are initializing the alphabets, we need a scheme which can guide the coder. ASCII table is the kind of scheme used in telecommunication for character encoding. Each character is a 7-bit code which represents an alphanumeric data. The latest version of this scheme was released in 1986. In total there are 256 characters that are present in this scheme. This includes both the uppercase and lowercase English alphabets along with 10 numerals and 33 special characters.

Ascii table structure

The entire ASCII table is classified into numerous categories. In total there are 256 ASCII characters but only 128 characters are represented. Out of these, 95 characters are considered to be printable.

Among the list of printable characters, 26 are lowercase alphabets ranging from 97-123. The 26 uppercase alphabets range from 65-91. Our aim is to draw information out of these 95 printable characters and specifically alphabetical characters.

Using the ORD() & chr() methods

The ord() method − This method is used returns the Unicode from a character in the ASCII table. Each character carries a Unicode and this Unicode is an integer value. The condition is that only a string of length 1 should be passed i.e., only a single character can be evaluated at once. We will use ord() function to set a range or fetch a Unicode for a particular character.

The Chr() method − This function returns a string or a character from a Unicode value. It receives a Unicode value as the parameter. We will use this function to retrieve characters after passing the range through ord() function.

Example

Following is a example to initialise alphabets into a list –

Here,

  • We created an empty list.

  • We initialized the starting character point in “char1” variable. This variable helps us to produce a reference for the ASCII table.

  • We created a for loop and iterated through the table to fetch all the 26 alphabets.

  • We used “chr()” to obtain the character for a Unicode variable.

  • We used “ord()” to obtain the Unicode character.

  • We increment the “char1” value, after every cycle.

  • At last, we print the fully initialized list.

lis1 = []
print("Contents of the list before initialisation:", str(lis1))
char1 = "A"
for characters in range(0, 26):
   lis1.append(char1)
   char1 = chr(ord(char1) + 1)
print("Contents of the list after initialisation:", str(lis1))

Output

Contents of the list before initialisation: []
Contents of the list after initialisation: ['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']

We can also print lower case alphabets by initializing char1 value as “a”.

Using the list comprehension

Using list comprehension, we can pass the codes in an elegant manner. The entire looping mechanism can be written in a single line. Let’s see its implementation −

Example

lis1 = []
print("the list before initialisation:", str(lis1))
lis1 = [chr(characters) for characters in range (ord("A"), ord("Z")+1)]
print("the list after initialisation:", str(lis1))

Output

the list before initialisation: []
the list after initialisation: ['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']

Using Mapping

In this method, we map the entire character table and then print it. Let’s see its implementation.

Here,

  • We created an empty list.

  • We mapped the list elements and used the ASCII character values to set a range. Since we are printing the uppercase values, we created a range of 65-91.

Example

lis1 = []
print("The list before initialisation:", str(lis1))
lis1 = list(map(chr, range(65, 91)))
print("The list after initialisation:", str(lis1))

Output

The list before initialisation: []
The list after initialisation: ['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']

Conclusion

In this article, we discussed the different ways in which we can initialize the entire English alphabets into a list. We understood the concept of ASCII scheme and its significance.

Updated on: 27-Feb-2023

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements