Convert List of String into Sorted List of Integer - Python
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    11 Jul, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                We are given a list of string a = ['3', '1', '4', '1', '5'] we need to convert all the given string data inside the list into int and sort them into ascending order so the output list becomes a = [1, 1, 3, 4, 5]. This can be achieved by first converting each string to an integer and then sorting the resulting list in ascending order. Various methods such as using map(), list comprehensions, loops and libraries like NumPy can be employed to perform this task.
Using map() and sorted()
We can convert a list of strings into a sorted list of integers using the map() and sorted() functions. map() function is used to apply int() to each string in the list and sorted() arranges the resulting integers in ascending order.
            Python
    a = ['3', '1', '4', '1', '5']
#all strings are converted into integers and sorted in ascending order
a = sorted(map(int, a))
print(a)
Explanation:
- map(int, li) converts each string in the list to an integer.
- sorted() sorts the resulting integers.
Using List Comprehension and sorted()
We can use a list comprehension and sorted() to convert a list of strings into a sorted list of integers. List comprehension converts each string to an integer using int() and sorted() arranges the integers in ascending order.
            Python
    a = ['3', '1', '4', '1', '5']
# Convert each string number to an integer and sort the list in ascending order
a = sorted([int(x) for x in a])  
print(a) 
Explanation:
- A list comprehension converts the strings to integers.
- sorted() then sorts the list of integers
Using int() in a for loop
We can use a for loop with int() to convert a list of strings into a sorted list of integers. First, iterate through  list to convert each string to an integer, store the results in a new list and then use sorted() to sort the integers.
            Python
    a = ['3', '1', '4', '1', '5'] 
b = [] 
# Iterate through each string in the list 'a'
for num in a:
    b.append(int(num))  # Convert each string to an integer and append to 'b'
    
 # Sort the list 'b' in ascending order
b.sort() 
print(b) 
 
Explanation:
- for loop iterates through each string in the list li, converts it to an integer using int(num) and appends the result to the list a.
- sort() method is then used to arrange the integers in the list a in ascending order, producing sorted list of integers.
Using numpy
We can use NumPy to convert a list of strings into a sorted list of integers by first using numpy.array() to create a NumPy array of integers. Then, numpy.sort() function is applied to sort the array in ascending order.
            Python
    import numpy as np
# List of strings representing integers
a = ["10", "2", "30", "4", "25"]
# Convert to NumPy array, sort, and convert back to list
b = np.sort(np.array(a, dtype=int)).tolist()
print(b)
Explanation:
- List li of strings is converted to a NumPy array of integers using np.array() with dtype=int and then sorted using np.sort().
- Sorted NumPy array is converted back to a Python list using .tolist() and printed.
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice