Convert All Strings in a List to Int in Python

In this tutorial,, you will learn how to convert all strings in a list to int in Python. Suppose you have a list with numbers as strings [“1”, “2”, “3”] and you want to convert this list of strings to a list of ints as [1,2,3].

There are basically two methods that you can use to convert the strings to ints. One is using the map function from Python and another one is using the list comprehension.

1. Using the Map Function in Python

The first method, and one that we strongly recommend, is to use Python’s Map function[1]. It accepts two arguments: an integer to which the string should be converted and a list of strings to which the string should be converted.

Let us see in the below example code the usage of the map function to convert a list of strings to int in Python.

#Initializing the List of Number in String
stringNumList = ["4", "5", "6", "7"]

#Using the Map function for conversion
getListNum = list(map(int, stringNumList))

#Printing the List of Map Function
print(getListNum)

Output:

[4, 5, 6, 7]

As you can see in the above code, the map function took two arguments one is the int to which you want the list of strings to convert, and the second one is the list of strings. Now the map function returns the list of integers but initially, it is in map object to convert that map object to the list object we are function the list function here.

2. Using List Comprehension Method

The second method that you can use to convert the list to an integer list is using the list comprehension method. This method is using the loop through the string elements one by one and typecasting them to integer values and then adding them back to the list.

Let us see in the below example code, how to use the list comprehension method to convert the list of a string into a list of integers in Python.

#Initializing the List of Number in String
stringNumList = ["8", "9", "10", "11"]

#Using the List Comprehension to Convert
getListNum = [int(listElement) for listElement in stringNumList]

#Printing the List of Map Function
print(getListNum)
[8, 9, 10, 11]

As you can see in the above code, using list comprehension I was able to convert the list to integers. But this one is slower than the above method.

3. Using the Naive Method to Convert All Strings in a List to Int

You can also convert it using the Naive method. Let us see in the below code to convert the list of strings to a list of integers using the Naive Method.

#Initializing the List of Number in String
stringNumList = ["8", "9", "10", "11"]

#Using Naive Method
listOfNumber = []

for item in stringNumList:
    listOfNumber.append(int(item))

#Print the List of Numbers
print(listOfNumber)
Convert All Strings in a List to Int

Output:

[8, 9, 10, 11]

Wrap Up

I hope you were able to get the answer on how to convert a list of strings into a list of integers using Python. I have listed down two methods that you can use.

Let me know in the comment section if you know a better method than the one discussed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Working With The Current Directory In Python
  2. Remove All Occurrences Of A Character In A String
  3. How To Convert GIF To Lottie JSON
  4. How To Get Every nth Element From A List in Python
  5. How To Run Python Script Constantly In Background
  6. How To Convert Python String To Array
  7. Code Example: How To Check If Set Is Empty In Python