Convert a List of Strings to a List of Integers in Python
To convert a list of strings to a list of integers in Python, we can use the map()
function, list comprehension, or a loop with the int()
function. These methods allow us to efficiently transform string representations of numbers into actual integer values.
Examples
1. Using map()
Function
The map()
function applies the int()
function to each element in the list, converting them from strings to integers.
# List of numeric strings
string_list = ["10", "20", "30", "40"]
# Converting to list of integers using map()
integer_list = list(map(int, string_list))
# Printing the result
print("Converted List:", integer_list)
Explanation:
In this example:
string_list
: A list of string representations of numbers.map(int, string_list)
: Applies theint()
function to each element instring_list
, converting them to integers.list()
: Converts the result frommap()
into a list.
Output:
Converted List: [10, 20, 30, 40]
2. Using List Comprehension
List comprehension allows us to iterate over the list and apply int()
to each element in a single line of code.
# List of numeric strings
string_list = ["5", "15", "25", "35"]
# Converting to list of integers using list comprehension
integer_list = [int(num) for num in string_list]
# Printing the result
print("Converted List:", integer_list)
Explanation:
In this example:
string_list
: Contains string numbers.[int(num) for num in string_list]
: Uses a loop within a list comprehension to applyint()
to each string.
Output:
Converted List: [5, 15, 25, 35]
3. Using a For Loop
We can manually iterate over the list using a for loop and convert each string to an integer.
# List of numeric strings
string_list = ["100", "200", "300", "400"]
# Converting to list of integers using a loop
integer_list = []
for num in string_list:
integer_list.append(int(num))
# Printing the result
print("Converted List:", integer_list)
Explanation:
In this example:
integer_list
: Starts as an empty list.- The loop iterates over
string_list
, converting each element usingint()
. - Each converted integer is added to
integer_list
usingappend()
.
Output:
Converted List: [100, 200, 300, 400]
4. Handling Non-Numeric Strings with try-except
If the list contains non-numeric values, we can use try-except
to handle conversion errors.
# List of numeric and non-numeric strings
string_list = ["10", "20", "abc", "30", "xyz"]
# Converting to list of integers with error handling
integer_list = []
for num in string_list:
try:
integer_list.append(int(num))
except ValueError:
print(f"Skipping non-numeric value: {num}")
# Printing the result
print("Converted List:", integer_list)
Explanation:
In this example:
try-except
: Attempts to convert each string to an integer.- If a non-numeric value is encountered,
ValueError
is caught and skipped.
Output:
Skipping non-numeric value: abc
Skipping non-numeric value: xyz
Converted List: [10, 20, 30]
Conclusion
To convert a list of strings to a list of integers in Python, you can use:
map()
: Best for concise and efficient conversion.- List Comprehension: A readable one-liner solution.
- For Loop: Useful for step-by-step conversion.
try-except
: Handles non-numeric values gracefully.
For simple cases, map()
or list comprehension is preferred. If handling errors, a loop with try-except
is the best choice.