HOWTO · Python

How to Convert List of Strings to Integer in Python

This tutorial demonstrates how to convert a list of strings to integers in Python using a list comprehension, map(), a for loop, or a NumPy integer array. It also explains how to handle decimal strings, mixed values, and inputs that cannot be converted.

On this page

Convert numeric strings with int() to build a list of integers. A list comprehension is a concise default; map() and a for loop are useful alternatives. For arrays, NumPy can convert the values when that library is already part of your project. The examples below use consistent input data. The supplied runtime results show the conversion producing [12, 7, 50, 92].

Convert a list of strings to integers

Each string must represent an integer accepted by int(), such as "12" or "-4". The conversion creates integer values, so you can use them in arithmetic without repeatedly parsing the strings. Choose a list comprehension for a straightforward transformation, map() when you want to apply a function across an iterable, or a loop when each item needs extra handling.

Use a list comprehension

A list comprehension expresses the conversion and collection in one readable expression. Run this example:

values = ["12", "7", "50", "92"]
integers = [int(value) for value in values]
print(integers)
[12, 7, 50, 92]

This creates a new list and leaves values unchanged. The list comprehension is a Python language construct, not a method. It is a good default when every input should be converted and the transformation is simple.

Use map()

map() applies int() to each item. In Python 3 it returns an iterator, so wrap it with list() when the result needs to be a list or printed as one:

values = ["12", "7", "50", "92"]
integers = list(map(int, values))
print(integers)
[12, 7, 50, 92]

This has the same conversion behavior as the preceding example. An iterator can be useful when processing values one at a time, but it is consumed as you iterate over it; materialize it when you need a reusable list. The function and list comprehension are both clear choices for ordinary conversion.

Use a for loop

A loop makes each step explicit and is convenient when conversion needs per-item logic, such as recording which source value failed. Append each converted value to a result list:

values = ["12", "7", "50", "92"]
integers = []
for value in values:
    integers.append(int(value))
print(integers)
[12, 7, 50, 92]

Use this form when a one-line comprehension would hide meaningful processing; for a direct conversion alone, the comprehension is shorter.

Convert strings with NumPy

If your project already uses NumPy and you need an array for numerical work, convert the strings with an integer dtype. This complete example imports NumPy, defines the input, and prints the resulting array; NumPy must be installed in the environment where you run it:

try:
    import numpy as np
except ImportError:
    print("Install NumPy to run this example.")
else:
    values = ["12", "7", "50", "92"]
    integers = np.array(values, dtype=int)
    print(integers)

The result is a NumPy integer array rather than a Python list. Use this approach when the surrounding work needs NumPy array operations. Performance and memory use depend on data size and workload, so measure your own case before making a speed comparison.

Handle values that cannot be converted

A string such as "seven" is not an integer representation, so int() raises ValueError. If the input may contain such values, catch the exception around each conversion and decide whether to skip the value, report it, or stop processing. For example, this version collects valid integers and records rejected strings:

values = ["12", "seven", "50"]
integers = []
rejected = []
for value in values:
    try:
        integers.append(int(value))
    except ValueError:
        rejected.append(value)
print(integers)
print(rejected)
[12, 50]
['seven']

Handle only the conversion error you expect; catching ValueError keeps unrelated programming errors visible. If every value must be valid, letting the exception propagate can be preferable because it makes bad input apparent. Validate or report rejected values at the point that best fits your application.

Convert decimal strings and mixed values

int() accepts integer-form strings, not decimal notation such as "3.8". If truncating the fractional part is the intended rule, convert through float() and then int():

values = ["3.8", "-2.4"]
integers = [int(float(value)) for value in values]
print(integers)
[3, -2]

Conversion to int truncates toward zero; it does not round to the nearest integer. If the task requires rounding, choose and apply an explicit rounding rule instead. Also consider precision limits when converting very large decimal strings through floating point.

For a list that already contains integers alongside numeric strings, convert only values that are strings:

values = [12, "7", 50]
integers = [int(value) if isinstance(value, str) else value for value in values]
print(integers)
[12, 7, 50]

This keeps existing integers as integers and parses the strings. The check does not make arbitrary objects or malformed strings safe: validate the actual input and handle ValueError when untrusted values may occur. If the list is already entirely integers, no conversion is needed.