5 Best Ways to Convert List of String to Sorted List of Integers in Python

πŸ’‘ Problem Formulation: How can we take a list of strings, each representing an integer, and convert it into a sorted list of integers? Suppose we have the input ['3', '1', '4', '1', '5']. The desired output is a list of integers sorted in ascending order: [1, 1, 3, 4, 5]. This article covers five methods to accomplish this in Python.

Method 1: Using sorted() and int()

Utilize Python’s built-in sorted() function and the int() constructor to convert each string to an integer and sort the list. This is straightforward and ideal for those new to Python.

Here’s an example:

str_list = ['3', '1', '4', '1', '5']
int_list = sorted([int(x) for x in str_list])
print(int_list)

The output of this code snippet:

[1, 1, 3, 4, 5]

This snippet uses a list comprehension to convert each string in the list to an integer. The sorted() function then sorts the resulting list of integers.

Method 2: Using map() Function

The map() function applies the int() function to each item of the list. Combined with sorted(), it offers a more functional programming approach.

Here’s an example:

str_list = ['3', '1', '4', '1', '5']
int_list = sorted(map(int, str_list))
print(int_list)

The output of this code snippet:

[1, 1, 3, 4, 5]

Here, map(int, str_list) returns an iterator that yields integers converted from the string list. The sorted() function sorts these integers.

Method 3: Using Lambda Function

A lambda function can be used as a key in the sorted() method to convert strings to integers during sorting, thus eliminating the need for a separate conversion step.

Here’s an example:

str_list = ['3', '1', '4', '1', '5']
int_list = sorted(str_list, key=lambda x: int(x))
print(int_list)

The output of this code snippet:

['1', '1', '3', '4', '5']

This approach sorts the strings directly, using a lambda as the key function that converts each string to an integer for comparison purposes.

Method 4: Using numpy Library

For those comfortable with external libraries, numpy can provide efficient array operations, including conversion and sorting of string arrays.

Here’s an example:

import numpy as np
str_list = ['3', '1', '4', '1', '5']
int_list = np.sort(np.array(str_list).astype(int))
print(int_list)

The output of this code snippet:

[1 1 3 4 5]

The code converts the list into a numpy array, changes the type to integer using astype(int), and then sorts the array with np.sort().

Bonus One-Liner Method 5: Using sorted() with int() in Place

A one-liner that modifies the original list in place, combining sort() with a list comprehension and int() conversion, can be a sleek way to achieve the task.

Here’s an example:

str_list = ['3', '1', '4', '1', '5']
str_list.sort(key=int)
int_list = [int(x) for x in str_list]
print(int_list)

The output of this code snippet:

[1, 1, 3, 4, 5]

This code modifies the original list of strings using sort() with a key function. After sorting, the list is converted to integers using a list comprehension.

Summary/Discussion

  • Method 1: List Comprehension with sorted(). Strengths: Easy to understand, succinct. Weaknesses: None for basic use cases.
  • Method 2: Using map() Function. Strengths: Functionally oriented, concise. Weaknesses: Slightly less readable for those unfamiliar with functional programming.
  • Method 3: Lambda Function in sorted(). Strengths: Operates in place, no need for list comprehension. Weaknesses: Produces sorted list of strings, not integers.
  • Method 4: Using numpy. Strengths: Fast for large datasets, familiar syntax for those who use numpy. Weaknesses: Requires additional library, may be overkill for simple tasks.
  • Method 5: Bonus One-Liner Sort In Place. Strengths: Modifies list in place, very compact. Weaknesses: Two-step process, could be confusing as it initially sorts strings.