Python | Ways to convert list of ASCII value to string
Last Updated :
08 Feb, 2024
Given a list of ASCII values, write a Python program to convert those values to their character and make a string. Given below are a few methods to solve the problem.
Method #1: Using Naive Method
Python3
ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 ,
111 , 114 , 71 , 101 , 101 , 107 , 115 ]
print ( "Initial list" , ini_list)
res = ""
for val in ini_list:
res = res + chr (val)
print ( "Resultant string" , str (res))
|
Output
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Time Complexity: O(n) where n is the number of elements in the list “ini_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #2: Using map()
Python3
ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 ,
111 , 114 , 71 , 101 , 101 , 107 , 115 ]
print ( "Initial list" , ini_list)
res = ''.join( map ( chr , ini_list))
print ( "Resultant string" , str (res))
|
Output
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Time Complexity: O(n) where n is the number of elements in the list “ini_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #3: Using join and list comprehension
Python3
ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 ,
111 , 114 , 71 , 101 , 101 , 107 , 115 ]
print ( "Initial list" , ini_list)
res = ''.join( chr (val) for val in ini_list)
print ( "Resultant string" , str (res))
|
Output
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Time Complexity: O(n) where n is the number of elements in the list “test_list”. Using join and list comprehension performs n number of operations.
Auxiliary Space: O(n), where n is the length of list
Method #4: Using reduce()
The reduce() function applies a function to an iterable, cumulatively reducing it to a single value. In this case, it applies a lambda function to each element in the list, where the lambda function takes in two arguments x and y. The lambda function concatenates the current element which is converted to character using chr() function with the next element, effectively joining all the elements in the list. The final output is a single string with all the characters in the list concatenated together.
Python3
from functools import reduce
ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 , 111 , 114 , 71 , 101 , 101 , 107 , 115 ]
print ( "Initial list" , ini_list)
res = reduce ( lambda x, y: x + chr (y), ini_list, '')
print ( "Resultant string" , res)
|
Output
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Time Complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(1) as we are not creating any new list and the concatenation is happening in place.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...