Python | Convert list of string into sorted list of integer
Last Updated :
20 Apr, 2023
Given a list of string, write a Python program to convert it into sorted list of integer.
Examples:
Input: ['21', '1', '131', '12', '15']
Output: [1, 12, 15, 21, 131]
Input: ['11', '1', '58', '15', '0']
Output: [0, 1, 11, 15, 58]
Let’s discuss different methods we can achieve this task.
Method #1: Using map and sorted()
Python3
list_string = [ '21' , '1' , '131' , '12' , '15' ]
list_map = map ( int , list_string)
list_sorted = sorted (list_map)
print (list_sorted)
|
Output:
[1, 12, 15, 21, 131]
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method #2: Using list comprehension
Python3
list_string = [ '11' , '1' , '58' , '15' , '0' ]
output = [ int (x) for x in list_string]
output.sort()
print (output)
|
Output:
[0, 1, 11, 15, 58]
Method #3: Using iteration
Python3
list_string = [ '11' , '1' , '58' , '15' , '0' ]
list_sorted = sorted ( int (x) for x in list_string)
print (list_sorted)
|
Output:
[0, 1, 11, 15, 58]
Method: Using enumerate function
Python3
lst = [ '11' , '1' , '58' , '15' , '0' ];l = []
for i,a in enumerate (lst):
l.append( int (a))
print ( sorted (l))
|
Output
[0, 1, 11, 15, 58]
Method #: Using lambda
Python3
list_string = [ '11' , '1' , '58' , '15' , '0' ]
sorted_list = [ int (i) for i in sorted (list_string, key = lambda x: int (x))]
print (sorted_list)
|
Output
[0, 1, 11, 15, 58]
The time complexity of this approach is O(n * log(n)), where n is the number of elements in the list. This is because the sorted function uses a sorting algorithm, such as quicksort or mergesort, which has a time complexity of O(n * log(n)) for sorting a list of n elements.
The space complexity of this approach is O(n), since a new list is created to store the sorted elements.
Method #: Using the sorted method and a key function
Python3
list_string = [ '21' , '1' , '131' , '12' , '15' ]
list_sorted = sorted (list_string, key = int )
list_sorted = [ int (x) for x in list_sorted]
print (list_sorted)
|
Output
[1, 12, 15, 21, 131]
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method#: Using Recursive method.
Python3
list_string = [ '21' , '1' , '131' , '12' , '15' ]
def sort_and_convert(lst):
if len (lst) < = 1 :
return [ int (lst[ 0 ])] if lst else []
else :
pivot = lst[ 0 ]
left = sort_and_convert([x for x in lst[ 1 :] if int (x) < = int (pivot)])
right = sort_and_convert([x for x in lst[ 1 :] if int (x) > int (pivot)])
return left + [ int (pivot)] + right
list_sorted = sort_and_convert(list_string)
print (list_sorted)
|
Output
[1, 12, 15, 21, 131]
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method #8: Using the built-in function sorted with a key argument
This method uses the built-in sorted function and specifies the key argument as int, which tells sorted to convert each element of the list to an integer before comparing and sorting them. This results in a sorted list of integers.
Python3
list_string = [ '21' , '1' , '131' , '12' , '15' ]
list_sorted = sorted (list_string, key = int )
print (list_sorted)
|
Output
['1', '12', '15', '21', '131']
Time complexity: O(n log n), where n is the length of the input list. This is because sorted uses a sorting algorithm that has a worst-case time complexity of O(n log n).
Auxiliary space: O(n), where n is the length of the input list. This is because sorted creates a new list to store the sorted elements, which has the same length as the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...