Python allows to work with various types of data such as “int”, “float”, etc., and the conversion of one data type into others i.e., “Typecasting” is a common task in Python. For instance, you may need to convert a list of strings to ints when there is a need to perform arithmetic operations on them or use them as indices.
This blog write-up will explore the ways to convert a strings list to ints in Python utilizing several examples.
How to Convert/Transform List of Strings to Integers in Python?
The below methods are utilized in Python to convert/transform a list of strings into an integers list:
Method 1: Convert List of Strings to Integers Utilizing the “for” Loop
The “for” loop can be utilized with the “int()” function to convert the string list into an integers list.
Example
Overview of the below code:
for i in range(0, len(list_of_strings)):
list_of_strings[i] = int(list_of_strings[i])
print(list_of_strings)
In the above code:
- The list of strings is initialized.
- The “for” loop iterates over the “range()” function starting from “0” and stops at the end of the strings list.
- Inside the “for” loop, each “string” value is converted into an “int” using the “int()” function.
Output
The input list of strings has been converted into a list of integers appropriately.
Method 2: Convert List of Strings to Integers Using “List Comprehension”
“List Comprehension” is a special way to create/construct a new list from the existing/current element of the list.
Syntax
Example
This code example shows the conversion of a strings list into a list of integers:
print('Given List of Strings: ', list_of_strings)
print('\nList of Ints: ', [int(i) for i in list_of_strings])
According to the above code, in the applied “List Comprehension” approach, the “for” loop iterates through the list of strings, and the “int()” function converts each element of the string list into integers.
Output
As seen, an integer list has been generated from the string input list.
Method 3: Convert List of Strings to Integers Using the “eval()” Function
In Python, the “eval()” function evaluates arbitrary Python expressions based on string or compiled code inputs. This function can be utilized with the “for” loop to convert the strings list to an integers list.
Syntax
In the above syntax, the “expression” parameter specifies the expression that needs to be evaluated, and the optional parameters “globals” and “locals” specify the global and local variables for the evaluation context, respectively.
Example
The below code block transforms a list of strings to an integers list:
print('Given List of Strings: ', list_of_strings)
print('\nList of Ints: ', [eval(i) for i in list_of_strings])
Based on the above code:
- A list of strings is initialized.
- The “eval()” function converts the element value of the strings list into integers by iterating through the list using the “for” loop.
Output
As seen, an integer list has been generated from the string input list.
Method 4: Convert List of Strings to Integers Utilizing the “map()” Function
The “map()” function processes and transforms all the items in an iterable without utilizing a traditional “for” loop, this practice is commonly referred to as “Mapping”. This function executes a specified function for each item in an iterable.
Syntax
In the above syntax, the “function” parameter specifies the function that is going to be applied to each item of the given iterable. The “iterable” parameter is iterable that we want to process.
Example
The following code transforms the strings list into an integers list:
print('Given List of Strings: ', list_of_strings)
print('\nList of Ints: ', list(map(int, list_of_strings)))
In the above code lines, the “map()” function takes “int” as a first argument and applies this function to all the elements of the given list of strings. The “list()” function converts the map object into an integer list using the “list()” function.
Output
Conclusion
To convert a list of strings to integers, various methods such as the “for” loop, “List Comprehension”, “eval()”, or the “map()” function is used in Python. These methods effectively convert string lists into lists of integers. This write-up delivered various approaches to convert/transform a list of strings to ints in Python using relevant examples.