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 map()
function if you know the contents of the original list will all convert to integers, otherwise use a lambda
function in your map()
, 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 map()
The built-in map(fn, iter)
takes 2 parameters: the first is the function (fn
) to modify each element in the second parameter’s iterable (iter
). The fn
needs just one parameter as the map()
function inserts each element from the iterable into the function.
If you have a list of strings that could all neatly change to integers you could solve this problem by inserting the int()
built-in method (which only takes one parameter) and using this as the first parameter to the map()
function.
Here’s how this would look:
|
|
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 print()
function with an asterisk operator on the map
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 list()
function.
Here’s what you would see inspecting the map object
:
|
|
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 list()
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 map()
function 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:
|
|
Where 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 int()
function:
|
|
As you can see trying to force conversion on an element that will not change to an integer using the int()
method will produce a ValueError
.
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.
Using lambda
With if
As Filter In map
Function
One means of being able to provide a filter on values being iterated through using the map
function is to replace fn
parameter (the first parameter in the map
function) which I’ve currently had as the int()
function is to change it with a custom lambda
function.
As the map()
function passes each iteration to the fn
function in the first parameter the lambda
function just needs one parameter, like so:
It’s then a matter of working on the right side of the lambda
call to produce what you want to return.
Our case here is to determine if the passed in value (represented by the parameter x
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 try-catch
One method of checking if a string will change to an integer without using a try
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:
|
|
As you can see from the two examples above, after importing the regex library using the import re
statement I created a string labelled a_string
containing all integers. Using the re.sub()
(regex substitute) method I inserted into its three parameters the regex pattern to find all digits (r'\d+'
), first, the empty string ''
to substitute for each digit found, second, and finally the string to perform the operation on.
By wrapping the re.sub()
with len()
I can then determine the length of the string remaining after 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 lambda
function would look something like this:
|
|
In this lambda
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 str.replace()
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 lambda
expression would look like without an import re
statement and using the str.replace()
approach:
|
|
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 lambda
function by writing the code len(x) > 0
. This would prevent any false positives going through.
|
|
As you can see the original variable a_var
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 1,000,000
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:
|
|
|
|
Testing the result of our code in the Python REPL would look something like this:
|
|
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 None
for any elements that couldn’t readily convert to an integer.
You could change the lambda
function to report the elements that couldn’t be changed by swapping the None
in the if-else
statement with x
, like this:
|
|
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 map()
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:
|
|
The biggest difference with the list comprehension compared to the map()
function is handling the else
cases. In the list comprehension above the if
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 for
statement.
Summary
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 None
or 0
in replacement.