Convert String List to ASCII Values - Python
Last Updated :
01 Feb, 2025
We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this.
Using List Comprehension with ord()
A simple and efficient way to convert strings to ASCII values is by using list comprehension along with the ord() function.
Python
a = ["Hi", "Bye"]
b = [[ord(c) for c in s] for s in a]
print(b)
Explanation:
- The ord() function returns the ASCII value of a character.
- We use a nested list comprehension to iterate through each string and convert its characters.
- This method is concise and runs efficiently.
Let's explore some more ways of converting string list to ascii values in Python.
Using map() with ord()
Another efficient approach is using the map() function, which applies ord() to each character in a string.
Python
a = ["Hi", "Bye"]
b = [list(map(ord, s)) for s in a]
print(b)
Explanation:
- The map() function applies ord() to each character in the string.
- Since map() returns a map object, we convert it into a list.
- This method is similar in efficiency to list comprehension but can be slightly more readable for some.
If we need a flat list of ASCII values instead of a nested list, we can use itertools.chain().
Python
from itertools import chain
a = ["Hi", "Bye"]
b = list(map(ord, chain(*a)))
print(b)
Explanation:
- chain(*a) flattens the list, treating all characters as a single sequence.
- map(ord, ...) applies ord() to each character.
- This method is useful when we want a flat list instead of a list of lists.
Using Dictionary Comprehension
If we want to keep track of original characters, we can store them in a dictionary.
Python
a = ["Hi", "Bye"]
b = [{c: ord(c) for c in s} for s in a]
print(b)
Explanation:
- Instead of just converting characters, this stores them as {char: ASCII} pairs.
- This method helps when we need both original characters and their ASCII values.
Using for Loop
We can use a standard for loop to iterate through each character in the list and convert it to ASCII values.
Python
a = ["Hi", "Bye"]
b = []
for s in a:
temp = []
for c in s:
temp.append(ord(c))
b.append(temp)
print(b)
Explanation:
- We initialize an empty list b to store the converted values.
- We loop through each string in the list and convert each character to ASCII using ord().
Similar Reads
Ways to Convert List of ASCII Value to String - Python The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting
3 min read
Convert Float String List to Float Values-Python The task of converting a list of float strings to float values in Python involves changing the elements of the list, which are originally represented as strings, into their corresponding float data type. For example, given a list a = ['87.6', '454.6', '9.34', '23', '12.3'], the goal is to convert ea
3 min read
Python Program to Convert a List to String In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it.Using the
3 min read
Convert String to Tuple - Python When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv
3 min read