5 Best Ways to Convert Python List of Strings to List of Integers

πŸ’‘ Problem Formulation:

As a Python developer, you might frequently encounter the need to process lists of strings and convert them to lists of integers. This can arise while parsing data from text files, handling user input, or working with APIs. The challenge is to efficiently transform a list like ['1', '2', '3'] into [1, 2, 3], handling potential exceptions gracefully.

Method 1: Using List Comprehension

List comprehension is a concise and Pythonic way to create lists. By utilizing this method, we transform each string in the list to an integer, resulting in a new list of integers.

Here’s an example:

str_list = ['4', '5', '6']
int_list = [int(item) for item in str_list]

Output:

[4, 5, 6]

This code snippet uses list comprehension to iterate over str_list, converting each element to an integer and creating a new list, int_list.

Method 2: Using the map() Function

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a map object (an iterator). We can apply int() to each element of the string list to obtain integers.

Here’s an example:

str_list = ['7', '8', '9']
int_list = list(map(int, str_list))

Output:

[7, 8, 9]

This code snippet uses map() to apply the int() function to each element in str_list, creating a map object that is then converted into a list of integers.

Method 3: Using a For Loop

A for loop can be used for transformation where you iterate through each string in the list and convert it into an integer, appending to a new list. This method is more verbose but also very explicit in its mechanics.

Here’s an example:

str_list = ['10', '11', '12']
int_list = []
for item in str_list:
    int_list.append(int(item))

Output:

[10, 11, 12]

In this code snippet, we iterate over str_list with a for loop, convert each item to an integer with int(), and append the result to int_list.

Method 4: Using ast.literal_eval()

The ast.literal_eval() function safely evaluates a string containing a Python literal or container display. It can be used to convert a list of string representations of numbers into actual integers.

Here’s an example:

import ast
str_list = ['13', '14', '15']
int_list = [ast.literal_eval(item) for item in str_list]

Output:

[13, 14, 15]

This code snippet uses list comprehension along with ast.literal_eval() to evaluate each string in str_list as a Python literal, effectively converting them to integers.

Bonus One-Liner Method 5: Using a Generator Expression with list()

A generator expression is similar to a list comprehension, but it creates an iterator instead of a list. This can be directly passed to list() to create the desired list of integers.

Here’s an example:

str_list = ['16', '17', '18']
int_list = list(int(item) for item in str_list)

Output:

[16, 17, 18]

Here, instead of creating an intermediate list as in list comprehension, a generator expression is used to convert each string to an integer on-the-fly when passed to list().

Summary/Discussion

  • Method 1: List Comprehension. It’s concise and Pythonic. However, it’s not as intuitive for beginners and can be less readable with complex conversions.
  • Method 2: map() Function. This method is functional and elegant. It may be less readable for those not familiar with functional programming paradigm and requires conversion to list.
  • Method 3: For Loop. The most explicit and easiest to understand for most programmers. It can be slower and more verbose than other methods.
  • Method 4: ast.literal_eval(). It’s secure and powerful. However, it can be slower and overkill for simple string to integer conversions, and inappropriate use can be unsafe.
  • Method 5: Generator Expression with list(). It’s memory-efficient for large data sets. However, it can be less intuitive than list comprehension and may lead to complex one-liners.