How to Convert a String List to an Integer List in Python

4.6/5 - (18 votes)

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings]. It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function.

This article shows you the simplest ways to convert a one-dimensional list consisting only of strings to a list of ints.

Problem: Given a list of strings ["1", "2", "-3"]. How to convert it to a list of ints [1, 2, -3]?

  • Problem Variant: Given a list of strings with mixed representations ["1", "2.0", "-3.4"]. How to convert it to a list of ints [1, 2, -3]?

We’ll dive into the easier base problem first and examine the problem variant in Method 5.

Method 1: List Comprehension

Suppose we have a list:

a = ["1", "2", "-3"]

Now, check the type of the first list element:

# <class 'str'>

Let’s apply the built-in function int(), and get a list of integers using list comprehension:

a = ["1", "2", "-3"]
print([int(x) for x in a])
# [1, 2, -3]

πŸ’‘ List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

A Simple Introduction to List Comprehension in Python

Check the type of numbers in the new list:

A = [int(x) for x in a]
# <class 'int'>

The built-in function int() converts a string to an integer. Thus, it helps us create a new list of ints from the list of strings in a single line of code.

Method 2: Map Function

The built-in function map is well-optimized and efficient, when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.

Apply to the same list a the following code:

a = ["1", "2", "-3"]
print(list(map(int, a)))
# [1, 2, -3]

πŸ’‘ The map() function applies the first argument, a function, to each element in an iterable. It transforms each element in the original iterable to a new element and returns a new iterable map object of transformed values. To obtain a list, you need to convert it using the built-in list() constructor.

You can watch my explainer video of the map function here:

Mastering the Python Map Function [+Video]

Method 3: For Loop

Of course, you can also convert a list of strings to a list of ints using a simple for loop. This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).

a = ["1", "2", "-3"]
ints = []

for element in a:

# [1, 2, -3]

This basic method to convert a list of strings to a list of integers uses three steps:

  • Create an empty list with ints = [].
  • Iterate over each string element using a for loop such as for element in list.
  • Convert the string to an integer using int(element) and append it to the new integer list using the list.append() method.

Method 4: List Comprehension + eval()

You can also use the eval() function in a list comprehension to convert a list of strings to a list of ints:

a = ["1", "2", "-3"]
ints = [eval(x) for x in a]
# [1, 2, -3]

πŸ’‘ Python’s built-in eval(s) function parses the string argument s into a Python expression, runs it, and returns the result of the expression. If the “expression” is a simple integer representation, Python converts the argument s to an integer.

You can watch me introduce the ins and outs of the eval() function in this short guide:

Python eval() -- How to Dynamically Evaluate a Code Expression in Python

Method 5: Mixed String Representation with Rounding

πŸ’¬ Problem Variant: Given a list of strings with mixed representations ["1", "2.0", "-3.4", "3.6"]. How to convert it to a list of ints [1, 2, -3, 4]?

The challenge is to convert each string to a float first and only then convert it to an integer.

These two steps are integral, and none can be skipped because you need the float to be capable of representing any number.

But you also need the integer, as this is the goal you set out to do: converting a list of strings to a list of ints.

❗ Convert a list of mixed string representations to a list of rounded integers this by chaining the built-in functions round() and float() in a list comprehension expression [round(float(s)) for s in a], assuming that the list of mixed string representations is stored in the variable a.

a = ["1", "2.0", "-3.4", "3.6"]
ints = [round(float(s)) for s in a]
# [1, 2, -3, 4]

You can learn everything about the round() function in the following video:

Python round() β€” A Helpful Interactive Guide

To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheets—if you haven’t already! πŸ™‚

If you want to go all-in and learn Python while getting paid in the process, check out my Python freelancer course—the number one freelance developer education in the world!

Programmer Humor

Q: How do you tell an introverted computer scientist from an extroverted computer scientist?

A: An extroverted computer scientist looks at your shoes when he talks to you.