How do you convert a list of strings to a list of integers in Python? And can you do it with one line of code?

To convert a list of strings to a list of integers use the built-in


function if you know the contents of the original list will
convert to integers, otherwise use a


function in your


, or use a list comprehension with conditions.

Let’s look at how you can change your original list using these approaches below.

Modify List Using


The built-in

map(fn, iter)

takes 2 parameters: the first is the function (


) to modify each element in the second parameter’s iterable (


). The


needs just one parameter as the


function inserts each element from the iterable into the function.

If you have a list of strings that could
neatly change to integers you could solve this problem by inserting the


built-in method (which only takes one parameter) and using this as the first parameter to the



Here’s how this would look:

>>> my_list = [‘1’, ‘2’, ‘3’]
>>> map(int, my_list)
<map object at 0x11beae770>

As you can see from the above result from my Python REPL the output is a

map object


To inspect the contents of the

map object

you could use the


function with an
asterisk operator
on the


function, but as this would make it hard to determine if there was an
actual change
to the elements I’m going to wrap the result in the



Here’s what you would see inspecting the

map object


>>> print(*map(int, my_list))
1 2 3
>>> list(map(int, my_list))
[1, 2, 3]
>>> print(my_list)
[‘1’, ‘2’, ‘3’]

From the above code, you can see that by using the
asterisk operator
it’s difficult to determine if the values have changed. However, using the


built-in function clearly shows the values in the new list are integers not strings – and you can see the difference between a list of strings (the original list) and the new list with how they are both output to the REPL.

What’s more notice how the original source list is
not modified
. Therefore the


does not mutate
the iterable being passed into its
second parameter

Therefore, to change a list of strings to a list of integers you could use the following one-liner:

list(map(int, string_list))



is the source variable that contains the list of strings.

But what if the original list of strings contains strings that cannot be converted to an integer?

Suppose you have the following list with an element within the list that cannot easily convert to an integer using the



>>> my_list = [‘1’, ‘2’, ‘e’]
>>> list(map(int, my_list))
Traceback (most recent call last):
File “<console>”, line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘e’

As you can see trying to force conversion on an element that will not change to an integer using the


method will produce a



This is where if it’s a possibility your source list may contain elements that will not readily convert to integers that you will need to apply filtering .





As Filter In



One means of being able to provide a filter on values being iterated through using the


function is to replace


parameter (the first parameter in the


function) which I’ve currently had as the


function is to change it with a custom



As the


function passes each iteration to the


function in the first parameter the


function just needs one parameter, like so:

lambda x: rest of function goes here

It’s then a matter of working on the right side of the


call to produce what you want to return.

Our case here is to determine if the passed in value (represented by the parameter


for each value from the list of strings) will convert to an integer, if not then you would likely discard it and move on.

How To Convert String To Integer Without Using


One method of checking
if a string will change to an integer
without using a


statement is to replace all the numbers in the string with an
empty string
and to see if there is anything left over.

The principle looks something like this:

>>> import re >>> a_string = '123' >>> len(re.sub(r'\d+', '', a_string)) 0 >>> b_string = '123A123' >>> len(re.sub(r'\d+', '', b_string)) 1

As you can see from the two examples above, after importing the regex library using the

import re

statement I created a string labelled


containing all integers. Using the


(regex substitute) method I inserted into its three parameters the regex pattern to find all digits (


), first, the
empty string


to substitute for each digit found, second, and finally the string to perform the operation on.

By wrapping the




I can then determine the length of the string remaining
the substitutions have happened. In my first example above the final string had 0 length as all characters in the string were digits, whereas the second example had a length of 1 as one character in the string was
not a digit

Using this same approach in the


function would look something like this:

lambda x: int(x) if len(x) > 0 and len(re.sub(r’\d+’, ”, x)) == 0 else None

In this


function above I’ve assumed import of the regex library. If you want to exclude importing a library you could make use of the built-in string method


which does the same thing, however, each digit would need to be chained together which makes the expression
very long

Here’s what the above


expression would look like without an

import re

statement and using the



lambda x: int(x) if len(x) > 0 and len(x.replace(‘0’, ”).replace(‘1’, ”).replace(‘2’, ”).replace(‘3’, ”).replace(‘4’, ”).replace(‘5’, ”).replace(‘6’, ”).replace(‘7’, ”).replace(‘8’, ”).replace(‘9’, ”) == 0 else None

As you can see when comparing both approaches the importing of the Regex library is a lot smaller, simpler and therefore less prone to mistakes!

Besides the substitution of the digit strings to empty strings I’ve also added a preliminary check to make sure a value is being passed into the


function by writing the code

len(x) > 0

. This would prevent any false positives going through.

>>> a_var = '' >>> int(a_var) if len(re.sub(r'\d+', '', a_var)) == 0 else None Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: ''

As you can see the original variable


holding an empty string can slip through the cracks if there’s no check initially on the length. Therefore, the

len(x) > 0

is needed to prevent this from happening.

What If Integer String Contains Thousands Separators (Like Comma Or Period)?

An integer string can still be valid but it may contain other characters for separation of thousands places such as


to denote one million. If this is needed for your use case then you can add in the substitution the commas or periods needed.

Here’s how this would look for each approach:

lambda x: int(x.replace(‘,’, ”)) if len(x) > 0 and len(re.sub(r’\d+|,’, ”, x)) == 0 else None

lambda x: int(x.replace(‘,’, ”)) if len(x) > 0 and len(x.replace(‘0’, ”).replace(‘1’, ”).replace(‘2’, ”).replace(‘3’, ”).replace(‘4’, ”).replace(‘5’, ”).replace(‘6’, ”).replace(‘7’, ”).replace(‘8’, ”).replace(‘9’, ”).replace(‘,’, ”) == 0 else None

Testing the result of our code in the Python REPL would look something like this:

>>> import re >>> a_list = ['1,234', '5678', 'ABCD'] >>> list(map(lambda x: int(x.replace(',', '')) if len(x) > 0 and len(re.sub(r'\d+|,', '', x)) == 0 else None)) [1234, 5678, None]

The outcome is achieved using the code above to help remove any thousand separators, such as a comma, and all in one line of code!

But notice the result produces a list the same size as the original with


for any elements that couldn’t readily convert to an integer.

You could change the


function to report the elements that couldn’t be changed by swapping the


in the


statement with


, like this:

>>> import re >>> a_list = ['1,234', '5678', 'ABCD'] >>> list(map(lambda x: int(x.replace(',', '')) if len(x) > 0 and len(re.sub(r'\d+|,', '', x)) == 0 else x)) [1234, 5678, 'ABCD']

Reporting the original elements back into the list could be helpful if more work is needed on converting the strings.

If the new list is to remove any elements that will not be converted to integers you can use another approach using list comprehensions.

Filter And Remove Elements Without Using


To filter elements from an original list based on certain conditions you can use the handy list comprehension.

With the current working examples here’s how this would work:

>>> a_list = ['1,234', '5678', 'ABCD'] >>> [int(x.replace(',', '')) for x in a_list if len(x) > 0 and len(re.sub(r'\d+|,', '', x)) == 0] [1234, 5678]

The biggest difference with the list comprehension compared to the


function is handling the


cases. In the list comprehension above the


condition filters each element according to the condition and if it does not meet this criteria the element is not passed through to the front of the




The list comprehension provides a simple one-liner piece of code that can easily convert a list of strings to integers and remove any elements within the list that cannot be easily converted to integers.

If it’s important to place something into the new integer list or to show elements from the original list that cannot be changed you could report back the original element or a default value such as




in replacement.

You are watching: Change List Elements To Int In Python: 1 Line Of Code. Info created by GBee English Center selection and synthesis along with other related topics.