The most time-saving and efficient way to list the ASCII characters is to use list comprehension. It is a method for creating a new list based on an existing list by performing a specific operation.
Since only 128 characters are in the ASCII character set, it is efficient to create a list in memory, which is why it is the fastest approach. However, if the list is extensive, this approach might not be a good fit for you.
Here’s the exact code implementation showing that:
ascii_characters = [chr(i) for i in range(128)] print(ascii_characters)
Output
As shown in the screenshot above, the output displays all ASCII characters in a list format on the console.
Each list element is an ASCII character. Performance-wise, this approach is the best.
One drawback of this approach is that you need to get familiar with list comprehension, which is more complex than using a simple for loop.
Method 2: Traditional for loop
In our case, when we are only dealing with 128 characters, it may be feasible to use a “for loop”.
However, I wouldn’t recommend using a for loop for every programming situation. It will cost you a performance.
ascii_characters = []
for ch in range(128):
ascii_characters.append(chr(ch))
print(ascii_characters)
Output
And we get the expected output, but this time using a “for loop”.
A key advantage of using a for loop is that you don’t need to take an extra step to learn any new programming concept. Traditionally, apply your standard logic using a for loop, and everything is set.
Method 3: String Module
Moving on from built-in methods, let’s discuss using a “string” module. The string module provides various props and methods to get the “pre-defined character sets,” which is extremely helpful when reading.
For example, you can use the “string.ascii_letters” that only returns the letters.
import string ascii_chars = list(string.ascii_letters + string.digits + string.punctuation + string.whitespace) print(ascii_chars)
Output
You can see from the output screenshot that we got what we wished for. However, it may include the characters you don’t need.
Additionally, they are limited to predefined sets, and they require us to import a “string” module in the first place, which, in some cases, is unnecessary for this task alone.
If you are interested in listing unicode point characters, check out this tutorial.



