Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Convert String list to ascii values

Python | Convert String list to ascii values

Sometimes, while working with Python, we can have a problem in which we need to convert String List to ascii values. This kind of problem can occur many times. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop + ord() This problem can be solved using above functionalities. In this, we iterate the list and convert each character to it’s ascii number using ord(). 

Python3




# Python3 code to demonstrate working of
# Convert String list to ascii values
# using loop + ord()
 
# initialize list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert String list to ascii values
# using loop + ord()
res = []
for ele in test_list:
    res.extend(ord(num) for num in ele)
 
# printing result
print("The ascii list is : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best']
The ascii list is : [103, 102, 103, 105, 115, 98, 101, 115, 116]

Time Complexity: O(N*N)

Auxiliary Space: O(N)

 Method #2 : Using list comprehension + ord() This is yet another way to perform this task. This is just shorthand to above problem in which we compact the code using list comprehension using similar logic. 

Python3




# Python3 code to demonstrate working of
# Convert String list to ascii values
# using list comprehension + ord()
 
# initialize list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert String list to ascii values
# using list comprehension + ord()
res = [ord(ele) for sub in test_list for ele in sub]
 
# printing result
print("The ascii list is : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best']
The ascii list is : [103, 102, 103, 105, 115, 98, 101, 115, 116]

Time Complexity: O(N*N)

Auxiliary Space: O(N)

Method#3: Using reduce() and map function + ord function: reduce is used to iterate over the list of string and Map function is used to convert the string to list of its ascii value by using ord as it’s function.  

Python3




# Python 3 code to convert
# string list to ascii value
# using reduce and map function
#importing reduce function
from functools import reduce
 
# list of string
test_str = ["gfg", "is", "best"];
 
# printing original list
print("Original list is : " + str(test_str))
 
# using reduce and map to convert
# list of string to ascii value
k = reduce(lambda x,y 😡 + list(map(ord, y)),test_str, [])
print("The ascii list is : ", str(k))


Output:

Original list is : ['gfg', 'is', 'best']
The ascii list is :  [103, 102, 103, 105, 115, 98, 101, 115, 116]

Time Complexity: O(N)

Auxiliary Space: O(N)

Approach#4: Using itertools.chain()

This approach imports the itertools module and defines a list original_list. It then uses map and lambda functions to convert each element of original_list to a list of its ASCII values, and flattens the resulting list using itertools.chain. The flattened list is then printed.

Algorithm

1. Define a lambda function that converts each character to its ASCII value using the ord() function.
2. Apply the lambda function to each character in each string in the original list using the map() function.
3. Flatten the resulting list of lists using the itertools.chain() function.
4. Return a new list of ASCII values.

Python3




import itertools
 
original_list = ['gfg', 'is', 'best']
ascii_list = list(itertools.chain(*map(lambda x: map(ord, x), original_list)))
 
print('The ascii list is:', ascii_list)


Output

The ascii list is: [103, 102, 103, 105, 115, 98, 101, 115, 116]

Time complexity: O(nm), where n is the length of the original list and m is the maximum length of a string in the list. We iterate over each character in each string.

Auxiliary Space: O(nm), where n is the length of the original list and m is the maximum length of a string in the list. We create a new list of lists using map(), and then flatten it using itertools.chain(). The final list ‘ascii_list’ stores n*m integers.

RELATED ARTICLES

Most Popular

Recent Comments