Given a List of Integers. The task is to convert them to List of Strings.
Examples:
Input: [1, 12, 15, 21, 131]
Output: ['1', '12', '15', '21', '131']
Input: [0, 1, 11, 15, 58]
Output: ['0', '1', '11', '15', '58']
Method 1: Using map()
Python3
list_int = [1, 12, 15, 21, 131]
list_string = map(str, list_int)
print(list(list_string))
Output
['1', '12', '15', '21', '131']
Time Complexity: O(n*nlogn) where n is the number of elements in the string list. The map() is used to perform the task and it takes O(n) time. Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list.
Example 2: Using list comprehension
Python3
list_string = [1, 12, 15, 21, 131]
output = [str(x) for x in list_string]
print(output)
Output
['1', '12', '15', '21', '131']
Method 3: Using iteration
Python3
list_string = [1, 12, 15, 21, 131]
list_int = []
for i in list_string:
list_int.append(i)
print(list_int)
Output
[1, 12, 15, 21, 131]
Method: Using enumerate function
Python3
lst = [1, 12, 15, 21, 131];l=[]
for i,a in enumerate(lst):
l.append(str(a))
print(l)
Output
['1', '12', '15', '21', '131']
Method 4: Using format function
Python3
list_int = [1, 12, 15, 21, 131]
output = [format(x, 'd') for x in list_int]
print(output)
Output
['1', '12', '15', '21', '131']
Time complexity: O(n) Auxiliary Space: O(n)
Method#5: Using Recursive method.
Algorithm:
Initialize a list list_int with some integer elements.
Define a recursive function convert_to_str() that takes a list of integers as input.
If the input list is empty, return an empty list.
Otherwise, convert the first element of the list to a string using str(), and append it to a new list created by calling
convert_to_str() on the remaining elements of the list (i.e. everything except the first element).
Call convert_to_str() on the sorted list of integers to convert all of the elements to strings.
Print the resulting list of sorted strings.
Python3
list_int = [1, 12, 15, 21, 131]
def convert_to_int(lst):
if len(lst) == 0:
return []
else:
return [str(lst[0])] + convert_to_int(lst[1:])
list_int_sorted = sorted(list_int)
list_int = convert_to_int(list_int_sorted)
print(list_int)
Output
['1', '12', '15', '21', '131']
Time Complexity: The time complexity of the recursive function convert_to_str() is O(N), where N is the length of the input list. This is because the function needs to process each element of the list exactly once. The sorted() function has a time complexity of O(N log N), where N is the length of the input list. Therefore, the overall time complexity of the algorithm is O(N log N).
Auxiliary Space: The space complexity of the recursive function convert_to_str() is O(N), where N is the length of the input list. This is because the function creates a new list to hold the converted strings, and the size of this list is proportional to the length of the input list. The space complexity of the sorted() function is also O(N), since it needs to create a new list to hold the sorted elements. Therefore, the overall space complexity of the algorithm is O(N).
Method 7: Using sorted() function with key parameter
Python3
list_int = [1, 12, 15, 21, 131]
list_string = sorted(map(str, list_int), key=int)
print(list_string)
Output
['1', '12', '15', '21', '131']
Time complexity: O(nlogn) – Sorting the list of integers takes O(nlogn) time and mapping each integer to a string takes O(n) time. Overall time complexity is dominated by the sorting operation.Auxiliary space: O(n) – A new list of strings is created with the same length as the original list of integers.
Method 8: Using functools.reduce() method
Approach:
Initialize list of intger.
Use reduce method on the initialized list which takes a function, original list, and initial value for the result as a parameter.
reduce method applies a function on each element of the original list and makes the final result.
Print the final result.
Python
from functools import reduce
s = [1, 12, 15, 21, 131]
Strlist = reduce(lambda a, b: a+[str(b)], s, [])
print(Strlist)
Output
['1', '12', '15', '21', '131']
Time Complexity: O(N). N is the length of list. Auxiliary space: O(1). Because no extra space is used.
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 :
22 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...