Skip to main content

How to Convert a String to a List of Integers in Python

A common task in data processing, such as reading from CSV files, user input, or API responses, is converting a string representation of numbers (e.g., "10 20 30") into an actual Python list of integers (e.g., [10, 20, 30]).

Since strings are sequences of characters, you cannot simply cast a sentence of numbers to an integer directly. You must first split the string into individual parts and then convert each part to an integer.

This guide covers the most efficient methods to achieve this using list comprehensions and the map() function, along with tips for handling custom delimiters.

Understanding the ValueError

A common mistake is attempting to cast a string containing spaces or multiple numbers directly to an int. Python's int() constructor expects a string representing a single number.

data = "10 20 30 40"

try:
# ⛔️ Incorrect: int() cannot parse spaces within the string
result = int(data)
print(result)
except ValueError as e:
print(f"Error: {e}")

Output:

Error: invalid literal for int() with base 10: '10 20 30 40'

To fix this, we must split the string into smaller substrings first.

The most "Pythonic" approach is to use the string .split() method combined with a list comprehension.

  1. string.split(): Breaks the string into a list of substrings based on whitespace.
  2. int(): Converts each substring.
data = "10 20 30 40 50"

# ✅ Correct: Split the string, then iterate and convert
number_list = [int(x) for x in data.split()]

print(f"List: {number_list}")
print(f"Type of elements: {type(number_list[0])}")

Output:

List: [10, 20, 30, 40, 50]
Type of elements: <class 'int'>
tip

.split() without arguments splits by any whitespace (spaces, tabs, newlines) and automatically removes empty strings caused by multiple spaces.

Method 2: Using the map() Function

The map() function applies a specific function (in this case, int) to every item in an iterable. This is often faster and preferred in functional programming styles.

data = "5 15 25 35"

# ✅ Correct: Apply 'int' to every item from split()
# Note: map returns an iterator, so we cast it to list()
iterator = map(int, data.split())
number_list = list(iterator)

print(f"List: {number_list}")

Output:

List: [5, 15, 25, 35]
note

If you forget list(), you will get a <map object at ...> instead of the actual list data.

Handling Custom Delimiters (Commas)

Input data is often separated by commas (CSV style) rather than spaces. You must pass the specific delimiter to the .split() method.

csv_data = "100,200,300,400"

# ✅ Correct: Split by comma
number_list = [int(x) for x in csv_data.split(',')]

print(f"List: {number_list}")

Output:

List: [100, 200, 300, 400]

Handling Spaces around Delimiters

If your data looks like "1, 2, 3" (spaces after commas), int() is actually smart enough to ignore the surrounding whitespace, so simple splitting works.

messy_data = "1,   2, 3"

# 'int()' automatically strips whitespace around the number strings
clean_list = [int(x) for x in messy_data.split(',')]

print(clean_list)

Output:

[1, 2, 3]

Converting a String of Single Digits

If you have a continuous string of numbers (e.g., "12345") and you want to split it into individual digits ([1, 2, 3, 4, 5]), you should iterate over the string directly rather than using split().

digit_string = "98765"

# ✅ Correct: Iterate over the string itself
digit_list = [int(char) for char in digit_string]

print(f"Digits: {digit_list}")

Output:

Digits: [9, 8, 7, 6, 5]

Conclusion

To convert a string to a list of integers:

  1. Use List Comprehension: [int(x) for x in s.split()] is the standard, readable choice.
  2. Use map(): list(map(int, s.split())) is a concise alternative.
  3. Check Delimiters: Use s.split(',') if your numbers are separated by commas.
  4. Single Digits: Iterate over the string directly if there are no separators.