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.

</>
Copy
# 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:

  1. string_list: A list of string representations of numbers.
  2. map(int, string_list): Applies the int() function to each element in string_list, converting them to integers.
  3. list(): Converts the result from map() 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.

</>
Copy
# 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:

  1. string_list: Contains string numbers.
  2. [int(num) for num in string_list]: Uses a loop within a list comprehension to apply int() 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.

</>
Copy
# 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:

  1. integer_list: Starts as an empty list.
  2. The loop iterates over string_list, converting each element using int().
  3. Each converted integer is added to integer_list using append().

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.

</>
Copy
# 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:

  1. try-except: Attempts to convert each string to an integer.
  2. 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:

  1. map(): Best for concise and efficient conversion.
  2. List Comprehension: A readable one-liner solution.
  3. For Loop: Useful for step-by-step conversion.
  4. 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.