Skip to content
Related Articles

Related Articles

Improve Article

Python | Convert list of string into sorted list of integer

  • Difficulty Level : Basic
  • Last Updated : 18 Feb, 2019

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




# 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




# 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




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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :