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.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
17 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...