Skip to main content

How to Split Strings into Lists of Integers in Python

This guide explains how to convert a string containing numbers (separated by spaces, commas, or other delimiters) into a list of integers in Python. We'll cover the most common and efficient methods, including list comprehensions, map(), for loops, and briefly touch on NumPy for specialized cases.

List comprehensions provide a concise and Pythonic way to split a string and convert its parts to integers:

my_str = '2 4 6 8 10'
list_of_integers = [int(x) for x in my_str.split(' ')]
print(list_of_integers) # Output: [2, 4, 6, 8, 10]
  • my_str.split(' '): Splits the string into a list of strings, using a space as the delimiter. If no argument is provided to split(), it splits on any whitespace.
  • [int(x) for x in ...]: This is the list comprehension. It iterates over the result of my_str.split(' '), and for each substring x, it converts it to an integer using int(x).

Handling Different Delimiters

If your numbers are separated by something other than a space, change the argument to split():

my_str = '2,4,6,8,10'
list_of_integers = [int(x) for x in my_str.split(',')] # Split on commas
print(list_of_integers) # Output: [2, 4, 6, 8, 10]

Handling Non-Digit Characters

If your string might contain non-digit characters, add a check using isdigit() within the list comprehension:

my_str = 'x y z 2 4 6 8 10 a'
list_of_strings = my_str.split(' ')
print(list_of_strings) # Output: ['x', 'y', 'z', '2', '4', '6', '8', '10', 'a']
list_of_integers = [int(x) for x in list_of_strings if x.isdigit()]
print(list_of_integers) # Output: [2, 4, 6, 8, 10]
  • if x.isdigit(): This ensures that only substrings consisting entirely of digits are converted to integers. Non-digit substrings are skipped.

Splitting and Converting with map()

The map() function provides another way to apply a function (in this case, int()) to each item in an iterable:

my_str = '2 4 6 8 10'
list_of_integers = list(map(int, my_str.split(' ')))
print(list_of_integers) # Output: [2, 4, 6, 8, 10]
  • my_str.split(' '): Splits the string into a list of strings.
  • map(int, ...): Applies the int() function to each string in the list, returning a map object (an iterator).
  • list(...): Converts the map object to a list.

While functional, list comprehensions are generally preferred for their readability in this scenario.

Splitting and Converting with a for Loop

You can use a for loop, although it's more verbose:

my_str = '2 4 6 8 10'
list_of_integers = []
for item in my_str.split(' '):
list_of_integers.append(int(item))
print(list_of_integers) # Output: [2, 4, 6, 8, 10]
  • This approach is less concise than list comprehensions or map().

Splitting and Converting with NumPy (for numerical data)

If you're working with numerical data and already using NumPy, np.fromstring() provides a specialized (and very fast) way to convert a string of numbers:

import numpy as np

my_str = '2 4 6 8 10'
my_list = np.fromstring(my_str, dtype=int, sep=' ').tolist()
print(my_list) # Output: [2, 4, 6, 8, 10]
  • np.fromstring(my_str, dtype=int, sep=' '): Converts the string directly to a NumPy array of integers, using space as the separator.
  • .tolist(): Converts the NumPy array to a standard Python list.
  • Use this method if you are already working with numerical data and NumPy.

Splitting into Digits Using re.findall()

If you want to split a string into its individual digits, regardless of separators, re.findall can be used, or even simpler, you can iterate over the string's characters:

my_str = '246810'
list_of_ints = [int(x) for x in my_str]
print(list_of_ints) # Output: [2, 4, 6, 8, 1, 0]
  • If your input contains only digits (no separators), you can directly iterate over the string.