Last modified: Sep 20, 2023 By Alexander Williams
10 Methods to Convert list of string to int python [Examples]
Example 1: Using a For Loop
# Initialize a list of strings representing numbers
str_list = ["10", "20", "30", "40", "50"]
# Initialize an empty list to store integers
int_list = []
# Use a for loop to convert strings to integers and append to the new list
for item in str_list:
int_list.append(int(item))
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [10, 20, 30, 40, 50]
Example 2: Using List Comprehension
# Initialize a list of strings representing numbers
str_list = ["5", "15", "25", "35"]
# Use list comprehension to convert strings to integers
int_list = [int(item) for item in str_list]
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [5, 15, 25, 35]
Example 3: Using the `map()` Function
# Initialize a list of strings representing numbers
str_list = ["100", "200", "300", "400"]
# Use the `map()` function to convert strings to integers
int_list = list(map(int, str_list))
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [100, 200, 300, 400]
Example 4: Using a Lambda Function with `map()`
# Initialize a list of strings representing numbers
str_list = ["7", "77", "777", "7777"]
# Define a lambda function to convert strings to integers
string_to_int = lambda x: int(x)
# Use the lambda function with `map()` to convert strings to integers
int_list = list(map(string_to_int, str_list))
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [7, 77, 777, 7777]
Example 5: Handling Error for Non-Integer Strings
# Initialize a list of strings with a mix of integers and non-integers
str_list = ["42", "hello", "123", "world", "987"]
# Initialize an empty list to store valid integers
int_list = []
# Use a try-except block to convert valid strings to integers
for item in str_list:
try:
int_list.append(int(item))
except ValueError:
pass # Skip non-integer strings
# Print the list of valid integers
print("Valid Integers:", int_list)
Output:
Valid Integers: [42, 123, 987]
Example 6: Handling Error and Providing Default Value for Non-Integer Strings
# Initialize a list of strings with a mix of integers and non-integers
str_list = ["42", "hello", "123", "world", "987"]
# Initialize an empty list to store integers or default values
int_list = []
# Define a default value for non-integer strings
default_value = 0
# Use a for loop to convert strings to integers or use the default value for non-integers
for item in str_list:
try:
int_list.append(int(item))
except ValueError:
int_list.append(default_value)
# Print the list of integers or default values
print("Integers or Defaults:", int_list)
Output:
Integers or Defaults: [42, 0, 123, 0, 987]
Example 7: Using List Comprehension with Conditional Conversion
# Initialize a list of strings representing numbers, some with commas
str_list = ["100", "1,000", "5", "10,000"]
# Use list comprehension to convert strings to integers, handling commas
int_list = [int(item.replace(',', '')) for item in str_list]
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [100, 1000, 5, 10000]
Example 8: Using a Function to Convert Strings to Integers
# Initialize a list of strings representing numbers
str_list = ["42", "87", "19", "56"]
# Define a function to convert a string to an integer
def convert_to_int(string):
try:
return int(string)
except ValueError:
return None # Handle non-integer strings as needed
# Use the function to convert strings to integers and filter out non-integer results
int_list = list(filter(None, map(convert_to_int, str_list)))
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [42, 87, 19, 56]
Example 9: Using a Generator Expression
# Initialize a list of strings representing numbers
str_list = ["3", "6", "9", "12"]
# Use a generator expression to convert strings to integers
int_generator = (int(item) for item in str_list)
# Convert the generator to a list of integers
int_list = list(int_generator)
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [3, 6, 9, 12]
Example 10: Using List of Hexadecimal Strings
# Initialize a list of hexadecimal strings
hex_str_list = ["1A", "2B", "3C", "4D"]
# Use list comprehension to convert hexadecimal strings to integers
int_list = [int(item, 16) for item in hex_str_list]
# Print the list of integers
print("Integers:", int_list)
Output:
Integers: [26, 43, 60, 77]