Open In App
Related Articles

Python Program to convert List of Integer to List of String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python code to convert list of
# string into sorted list of integer
   
# List initialization
list_int = [1, 12, 15, 21, 131]
   
# mapping
list_string = map(str, list_int)
   
# Printing sorted list of integers
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

# Python code to convert list of 
# string into sorted list of integer
   
# List initialization
list_string = [1, 12, 15, 21, 131]
   
# Using list comprehension
output = [str(x) for x in list_string]
   
# Printing output
print(output)

                    

Output
['1', '12', '15', '21', '131']

Method 3: Using iteration 

Python3

# Python code to convert list of
# string into sorted list of integer
   
# List initialization
list_string = [1, 12, 15, 21, 131]
   
   
list_int = []
# using iteration and sorted()
for i in list_string:
    list_int.append(i)
   
# printing output
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

# Method 4: Using format function
 
# Python code to convert list of
# integer into list of strings
 
# List initialization
list_int = [1, 12, 15, 21, 131]
    
# Using format function
output = [format(x, 'd') for x in list_int]
    
# Printing output
print(output)
#This code is contributed by Edula Vinay Kumar Reddy

                    

Output
['1', '12', '15', '21', '131']

Time complexity: O(n)
Auxiliary Space: O(n)
 

Method#5: Using Recursive method.

Algorithm:

  1. Initialize a list list_int with some integer elements.
  2. Define a recursive function convert_to_str() that takes a list of integers as input.
  3. If the input list is empty, return an empty list.
  4. Otherwise, convert the first element of the list to a string using str(), and append it to a new list created by calling
  5. convert_to_str() on the remaining elements of the list (i.e. everything except the first element).
  6. Call convert_to_str() on the sorted list of integers to convert all of the elements to strings.
  7. Print the resulting list of sorted strings.
     

Python3

# Python code to convert list of
# string into sorted list of string
 
# List initialization
list_int = [1, 12, 15, 21, 131]
 
# Define a recursive function to convert the list of strings to a list of integers
def convert_to_int(lst):
    if len(lst) == 0:
        return []
    else:
        return [str(lst[0])] + convert_to_int(lst[1:])
 
# Sort the list of integers
list_int_sorted = sorted(list_int)
 
# Call the recursive function to convert the list of strings to a list of integers
list_int = convert_to_int(list_int_sorted)
 
 
 
# Printing sorted list of integers
print(list_int)
#this code contributed by tvsk

                    

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

# Python code to convert list of
# string into sorted list of integer
from functools import reduce
 
# List initializationfrom functools import reduce
s = [1, 12, 15, 21, 131]
 
# converting integer list to list of string
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.  



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads