Convert List of String into Sorted List of Integer - Python
Last Updated : 21 Feb, 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.
Similar Reads
Python | Convert list of numerical string to list of Integers Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss
5 min read
Python | Convert List of String List to String List Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd
6 min read
Python | Convert list into list of lists Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202],
5 min read
Convert List to Delimiter Separated String - Python The task of converting a list to a delimiter-separated string in Python involves iterating through the list and joining its elements using a specified delimiter. For example, given a list a = [7, "Gfg", 8, "is", "best", 9] and a delimiter "*", the goal is to produce a single string where each elemen
3 min read
Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa
3 min read
Python | Convert given list into nested list Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let's discuss certain ways in which this can be performed. Convert the
4 min read