Skip to content
Related Articles

Related Articles

Python | Convert list of string into sorted list of integer

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 06 Aug, 2022

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]

  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]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!