Saturday, December 28, 2024
Google search engine
HomeLanguagesPython | Convert list of string into sorted list of integer

Python | Convert list of string into sorted list of integer

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




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




# Python code to convert list of
# string into sorted list of integer
 
# List initialization
list_string = ['11', '1', '58', '15', '0']
 
# Using list comprehension
output = [int(x) for x in list_string]
 
# using sort function
output.sort()
 
# Printing output
print(output)


Output:

[0, 1, 11, 15, 58]

  Method #3: Using iteration 

Python3




# Python code to convert list of
# string into sorted list of integer
 
# List initialization
list_string = ['11', '1', '58', '15', '0']
 
# using iteration and sorted()
list_sorted = sorted(int(x) for x in list_string)
 
# printing output
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




# Python code to convert list of
# string into sorted list of integer
# List initialization
list_string = ['11', '1', '58', '15', '0']
# sorting list
sorted_list = [int(i) for i in sorted(list_string, key=lambda x: int(x))]
print(sorted_list)
#This code is contributed by Edula Vinay Kumar Reddy


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)
#This code is contributed by Vinay pinjala.


Output

[1, 12, 15, 21, 131]

Time Complexity: O(nlogn)

Auxiliary Space: O(n)

Method#: Using Recursive method.

Python3




# List initialization
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
 
# sorting list
list_sorted = sort_and_convert(list_string)
 
# Printing sorted list of integers
print(list_sorted)
#this code contributed by tvsk


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.

RELATED ARTICLES

Most Popular

Recent Comments