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. 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)
[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)
[8, 9, 10, 11]
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.
- Working With The Current Directory In Python
- Remove All Occurrences Of A Character In A String
- How To Convert GIF To Lottie JSON
- How To Get Every nth Element From A List in Python
- How To Run Python Script Constantly In Background
- How To Convert Python String To Array
- Code Example: How To Check If Set Is Empty In Python