Are you looking for a way to convert a list of strings to ints in Python? You are in the right place. This tutorial will show you how to do it
You can convert a list of strings to a list of integers in Python with a for loop that goes through each item in the list of strings, converts it into an int, and appends it to a new list. A more concise alternative is to use a list comprehension. A third option is using the map() function to convert each string in the list to an int and then pass the output of the map() function to the list() function.
At the end of this tutorial, you will know how to convert the elements in a list from the string to the int data type.
Let’s see how these different implementations work in practice!
Convert a List of Strings to a List of Integers Using a Python For Loop
We will start with the most obvious approach that uses a for loop to go through each element of the list of strings, converts it to an int, and then appends that number to an empty list.
When using this approach, we create a new empty list outside of the for loop. This empty list will be our final list of integers.
list_of_strings = ['1', '5', '7', '45', '67']
list_of_ints = []
for item in list_of_strings:
list_of_ints.append(int(item))
print(list_of_ints)
The variable list_of_strings represents a list of strings. It’s a list because it starts and ends with square brackets. It’s a list of strings because every item in the list is a string (it’s enclosed within single quotes).
In the first line of code, we create the list of strings. In the second line of code, we create an empty list that will be our final list of integers.
Then we write a for loop that goes through each element of the list of strings and converts each item to an integer using the Python int() built-in function.
After converting each string in the list to an int we add the integer to the list variable list_of_ints using the list append() method.
In the last line, we use the print() function to print the final list of integers. Execute this code and confirm that the output is correct:
[1, 5, 7, 45, 67]
Let’s explore a second way to convert lists of strings to ints in Python.
Convert List of Strings to List of Ints with Python List Comprehension
Another way to convert lists of strings to lists of ints in Python is by using a list comprehension. You will see that with this approach we can reduce the length of our Python code to a single line.
The first two differences with this approach compared to the previous one are that we don’t have to create a new empty list and we don’t need a for loop.
list_of_strings = ['1', '5', '7', '45', '67']
list_of_ints = [int(item) for item in list_of_strings]
print(list_of_ints)
You can see that when using list comprehensions the final list of ints is created with a single line of code.
The internal logic of a list comprehension replaces the logic previously implemented using a for loop.
If you are just getting started with Python it might take you some time to get your head around the syntax of a list comprehension. Don’t worry, it’s a very common challenge that many Python developers experience.
Once again we confirm that the output of this Python code is correct:
[1, 5, 7, 45, 67]
Let’s move to the third approach.
Converting a Python List of Strings to Integers Using the Map(), and List() Functions
This approach is more complex than the previous ones so I wouldn’t necessarily use it in a real-world scenario. At the same time, it’s a way to learn all the options you have available with Python.
First of all, we use the map() function to convert each string in the list into an int.
map(int, list_of_strings)
As you can see, the map() function takes another function as the first argument. In this example the function we pass to the map() function as the first argument is the int() function. This function is applied to every item in the list (or iterable) passed as a second argument to map().
Let’s print the output of the map() function to see what we get back:
print(map(int, list_of_strings))
[output]
<map object at 0x1050c74c0>
Interesting…
The map() function returns a map() object.
So, how can we get back a list of ints?
We simply have to pass the object returned by the map() function to the Python list() built-in function as you can see below.
list_of_strings = ['1', '5', '7', '45', '67']
list_of_ints = list(map(int, list_of_strings))
print(list_of_ints)
The output is correct:
[1, 5, 7, 45, 67]
Well done!
In all these examples, I’m using Python 3. If for some reason you are using Python 2, you don’t need to use the list() function in this specific case.
list_of_strings = ['1', '5', '7', '45', '67']
list_of_ints = map(int, list_of_strings)
print(list_of_ints)
You can see that the output is correct without using the list() function.
python2 convert_list_of_strings_to_ints.py
[output]
[1, 5, 7, 45, 67]
That’s because in Python 2 the map() function returns a list instead of a map() object.
Using a While Loop to Convert a Python List of Strings to a List of Integers
We have another way to convert a list of strings to ints in Python!
You can use a while loop. The principle is similar to the for loop. We use the while loop to:
- Go through each item in the list of strings.
- Convert that item to an integer.
- Use the list append() method to add the integer at the end of the new list of ints we create outside of the while loop.
Here is the Python code for this approach:
list_of_strings = ['1', '5', '7', '45', '67']
list_of_ints = []
index = 0
while index < len(list_of_strings):
list_of_ints.append(int(list_of_strings[index]))
index += 1
print(list_of_ints)
The main difference compared to the approach using the for loop is that we create the variable index outside of the while loop.
We set the initial value of the variable index to zero and then we increase its value by one at every iteration of the while loop. When the value of the index becomes 5 the condition of the while loop becomes false and this terminates the execution of the while loop.
The condition of the while loop becomes false because the length of the list_of_strings is 5:
list_of_strings = ['1', '5', '7', '45', '67']
print(len(list_of_strings))
[output]
5
The result of the code using the while loop is also correct:
[1, 5, 7, 45, 67]
Conclusion
In this Python tutorial, we have seen multiple ways to convert a list of strings to ints in Python.
As we have seen, you can use a for loop or while loop to iterate explicitly through the list of strings. Alternatives to these approaches are list comprehensions or the map() function.
You might be using this knowledge for a program you are creating right now or it’s something you will use in any of the programs you will create in the future.
Either way, knowing this will give you more techniques to solve problems when writing programs using the Python programming language.
Well done for improving your Python skills!
Related articles: one of the ways we have seen to generate a list of ints is list comprehensions. To familiarise yourself with them go through the Codefather tutorial about Python list comprehensions.

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!