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
# Python code to demonstrate # conversion of list of ascii values # to string # Initialising list ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 , 111 , 114 , 71 , 101 , 101 , 107 , 115 ] # Printing initial list print ( "Initial list" , ini_list) # Using Naive Method res = "" for val in ini_list: res = res + chr (val) # Printing resultant string print ( "Resultant string" , str (res)) |
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] Resultant string Lazyroar
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
# Python code to demonstrate # conversion of list of ascii values # to string # Initialising list ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 , 111 , 114 , 71 , 101 , 101 , 107 , 115 ] # Printing initial list print ( "Initial list" , ini_list) # Using map and join res = ''.join( map ( chr , ini_list)) # Print the resultant string print ( "Resultant string" , str (res)) |
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] Resultant string Lazyroar
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
# Python code to demonstrate # conversion of a list of ascii values # to string # Initialising list ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 , 111 , 114 , 71 , 101 , 101 , 107 , 115 ] # Printing initial list print ( "Initial list" , ini_list) # Using list comprehension and join res = ''.join( chr (val) for val in ini_list) # Print the resultant string print ( "Resultant string" , str (res)) |
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] Resultant string Lazyroar
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 # Initialising list ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 , 111 , 114 , 71 , 101 , 101 , 107 , 115 ] # Printing initial list print ( "Initial list" , ini_list) # Using reduce res = reduce ( lambda x, y: x + chr (y), ini_list, '') # Printing resultant string print ( "Resultant string" , res) #This code is contributed by Edula Vinay Kumar Reddy |
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] Resultant string Lazyroar
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.