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()

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:



[1, 12, 15, 21, 131]

 
Method #2: Using list comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[0, 1, 11, 15, 58]

 
Method #3: Using iteration

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[0, 1, 11, 15, 58]


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.