How to convert a list of strings to ints in Python? You can use list comprehension or the map()
function to convert a list of strings to ints in one line very efficiently. You can use other ways of Python to convert a list of strings to integers like for
loop, eval()
, ast.literal_eval()
, and numpy.array()
functions. In this article, I will explain how to convert a list of strings to ints by using all these functions with examples.
Related: You can also convert a list of integers to a string in Python.
1. Quick Examples of Converting List of Strings to Ints
If you are in a hurry, below are some quick examples of converting a list of strings to ints.
# Quick examples of convert list of strings to ints
import ast
import numpy as np
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
# Example 1: Using for loop
# Convert list of strings to integers
for i in range(0, len(string_list)):
string_list[i] = int(string_list[i])
# Example 2: Using list comprehension
# Convert list of strings to integers
int_list = [int(x) for x in string_list]
# Example 3: Using map() function
# Convert list of strings to integers
int_list = list(map(int, string_list))
# Example 4: Using eval() function
# Convert list of strings to integers
int_list = [eval(i) for i in string_list]
# Example 5: Using ast.literal_eval() function
# Convert list of strings to integers
int_list = [ast.literal_eval(i) for i in string_list]
# Example 6: Using numpy.array() function
my_array = np.array(string_list, dtype=int)
int_list = list(my_array)
2. Convert List of Strings to Integers Using For Loop
To convert a list of strings to integers using a for loop in Python. For instance, you can initialize a list of strings string_list
containing numerical values as strings. Then you can iterate through each element string_list
using a for
loop and convert each element from a string to an integer using the int() function. The converted integers are stored back in the original list. After this loop, the string_list
contains integers instead of strings.
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:", string_list)
# Using for loop
# Convert list of strings to integers
for i in range(0, len(string_list)):
string_list[i] = int(string_list[i])
print("After converting a list of strings to integers:", string_list)
Yields below output.
This shows that the list of strings has been successfully converted to a list of integers.
3. Convert List of Strings to Integers Using List Comprehension
You can also convert a list of strings to integers using list comprehension. In this code, the list comprehension [int(x) for x in string_list]
iterates through each element x
in string_list
and converts it to an integer using the int()
function. The resulting list of integers is stored in the variable int_list
.
List comprehension provides a concise and readable way to convert a list of strings to a list of integers.
Related: You can convert a list to an integer in Python.
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:",string_list)
# Using list comprehension
# Convert list of strings to integers
int_list = [int(x) for x in string_list]
print("After converting a list of strings to integers:", int_list)
Yields the same output as above.
4. Convert List of Strings to Integers Using map() Function
Alternatively, you can use the map() function to convert a list of strings to integers and then convert the map object back to a list. For instance, the map(int, string_list)
applies the int()
function to each element string_list
, converting them to integers. The map()
function returns a map object, which is then converted back to a list using the list() function.
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:",string_list)
# Using map() function
# Convert list of strings to integers
int_list = list(map(int, string_list))
print("After converting a list of strings to integers:", int_list)
Yields the same output as above.
5. Convert List of Strings to Integers Using eval() Function
To convert a list of strings to integers using the eval()
function in Python. For instance, you can initialize a list of strings containing numeric values, and then use the eval()
function to convert these strings to integers.
In this code, the eval()
function is used inside a list comprehension to evaluate each element string_list
and convert it into an integer. The resulting int_list
contains the converted integers.
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:",string_list)
# Using eval() function
# Convert list of strings to integers
int_list = [eval(i) for i in string_list]
print("After converting a list of strings to integers:", int_list)
Yields the same output as above.
6. Using the ast.literal_eval() function from the ast Module
To convert a list of strings representing integers into a list of actual integers using the ast.literal_eval()
function. For instance, first, import the ast
module, which provides a safe way to evaluate and parse expressions using the Python literal syntax. Then initializes a list of strings where each string represents a number.
Uses a list comprehension to iterate through each string string_list
and apply ast.literal_eval()
to convert the string to its corresponding integer representation. The resulting integers are stored in the int_list
variable.
import ast
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:",string_list)
# Using ast.literal_eval() function
# Convert list of strings to integers
int_list = [ast.literal_eval(i) for i in string_list]
print("After converting a list of strings to integers:", int_list)
Yields the same output as above.
7. Using numpy.array() Function
Finally, you can use NumPy, a powerful library for numerical computing in Python, to convert a list of strings to integers. In this code, the np.array()
function from NumPy is used to create a NumPy array of integers from the string_list
. The dtype=int
argument ensures that the elements are treated as integers. Then, the NumPy array is converted back to a Python list using the list()
function, resulting in int_list
containing integers.
import numpy as np
# Initialize the list of strings
string_list = ["3", "6", "12", "9", "18"]
print("Original string:",string_list)
# Using numpy.array() function
my_array = np.array(string_list, dtype=int)
int_list = list(my_array)
print("After converting a list of strings to integers:", int_list)
Yields the same output as above.
Conclusion
In this article, I have explained how to convert a list of strings to ints in Python by using for
loop, list comprehension, map()
, eval()
, ast.literal_eval()
, and numpy.array()
functions with examples.
Happy Learning !!
Related Articles
- Convert String to Decimal in Python
- Python String isnumeric
- How to Parse a String in Python
- Compare strings using either ‘==’ or ‘is’ sometimes produces a different result.
- Python Extract Numbers From String
- Compare Strings in Python
- Check if String is Empty or Not in Python
- Convert Integer to String in Python
- Trim Whitespace from a String in Python?
- Convert String to Lowercase in Python