Python list of strings means that each of the element values in a list is initialized as a string. While a list of integers means that each item/element value contains the integers. To perform the numerical operation on the string list such as sorting, arithmetic, etc we need to convert the string list to an integer list.
This post will explain various methods for converting a string list to an integer list in Python:
- Method 1: Using map() Function With list()
- Method 2: Using the for Loop
- Method 3: Using the List Comprehension
- Method 4: Using the eval() Method
- Method 5: Using the round() Function
- Method 6: Using the ast.literal_eval() Function
Method 1: Using map() Function With list()
The “map()” function is utilized to apply a specific function on every value of the given sequence. The “map()” function is used with the combination of the list() function to convert a string list to an integer list:
Code:
list_value = ['15', '25', '30']
print(list(map(int, list_value)))
- The “map()” function takes the “int” and given list as a parameter and applies the integer function on each of the elements of the list.
- The returned map object is then passed to the “list()” function to convert it into the list.
Output:
The given string list has been converted into an integer list.
Method 2: Using the for Loop
The standard for loop is utilized along with the “append()” and “int()” functions to iterate through the string list and convert it into an integer list:
Code:
list_value = ['5', '15', '25', '35', '45', '55']
new_list = []
for i in list_value:
new_list.append(int(i))
print(new_list)
- The string list and empty list are initialized in the program.
- In the for loop, the “int()” function converts each of the list elements into integers and passes it as an argument to the “append()” function.
- After that, the “append()” function appends an integer into an empty list.
Output:
The string list has been converted.
Method 3: Using List Comprehension
The list comprehension is used with the “int()” function to generate a new list containing integer:
Code:
list_value = ['15', '25', '30']
print ([int(items) for items in list_value])
The “int()” function converts each element of the list into an integer and returns a new list containing an integer.
Output:
String List has been converted to integer list.
Method 4: Using the eval() Method
The “eval()” is used to evaluate the string as a Python expression and retrieve the outcomes. In the below code, the “eval” function is used along with the list comprehension to convert a string list to an integer list.
Code:
list_value = ['15', '25', '35', '45', '55']
print([eval(items) for items in list_value])
In the list comprehension, the “eval()” function takes the elements of a given string list and returns the integer.
Output:
The string list has been converted into an integer list.
Method 5: Using the round() Function
The “round()” function is used along with the “float()” function in the list comprehension to convert a Python list of strings to an integer list. Here is an example code:
Code:
list_value = ['1.5', '2.7', '35', '55']
print([round(float(items)) for items in list_value])
- The list containing mixed strings such as float and integer is initialized in the program.
- The “float()” function takes the elements of a given string list and converts them into floating point numbers.
- The “round()” function accepts all the floating point numbers and returns the nearest integer.
- The combination of these methods will convert the Python string list to an integer list.
Output:
The given string has been converted into integers.
Method 6: Using ast.literal_eval() Function
The “ast.literal_eval()” function is used to evaluate the string containing Python literals. The below code uses the “ast.literal_eval()” function inside the list comprehension to convert a string list to an integer list:
Code:
import ast
list_value = ['22', '33', '44', '55']
print([ast.literal_eval(items) for items in list_value])
- The “ast” module is imported.
- For each iteration, the “ast.literal_eval()” function converts the string list element into an integer.
- The new list is created by the list comprehension that contains the element value returned by the “ast.literal_eval()” function.
Output:
The input string list has been converted into a list of integers.
Conclusion
The map(), for loop, list comprehension, eval(), round(), and ast.literal_eval() functions are used to convert a string list to an integer list in Python. The “map()” function is used along with the “list()” function to convert the given list of strings into a list of integers. The “eval()”, “round()”, and other methods are also efficient methods to convert a string list to an integer list.
This guide has presented multiple methods to convert a Python string list to an integer list using various examples.