Split a string into a list of integers in Python

avatar

Borislav Hadzhiev

Last updated: Jun 23, 2022

banner

Check out my new book

Split a string into a list of integers in Python #

To split a string into a list of integers:

  1. Use the str.split() method to split the string into a list of strings.
  2. Use the map() function to convert each string into an integer.
  3. Use the list() class to convert the map object to a list.
main.py
# 👇️ string containing integers with space-separator my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # 👉️ ['2', '4', '6', '8', '10'] list_of_integers = list(map(int, list_of_strings)) print(list_of_integers) # 👉️ [2, 4, 6, 8, 10] # -------------------------- # 👇️ if you want to split the string on each digit my_str_2 = '246810' list_of_ints = [int(x) for x in my_str_2] print(list_of_ints) # 👉️ [2, 4, 6, 8, 1, 0]
If the integers in your string are separated with another delimiter, e.g. a comma, pass a string containing a comma to the str.split() method.

We used the str.split() method to split the string into a list of strings.

main.py
my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # 👉️ ['2', '4', '6', '8', '10']

The str.split() method splits the original string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

The next step is to convert each string in the list to an integer using the map() function.

main.py
my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # 👉️ ['2', '4', '6', '8', '10'] list_of_integers = list(map(int, list_of_strings)) print(list_of_integers) # 👉️ [2, 4, 6, 8, 10]

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

On each iteration, we pass the string to the int() class to convert it to an integer.

The map() function returns a map object, so we have to use the list() class to convert it to a list.

You can also use a list comprehension to convert a list of strings to a list of integers.

main.py
my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # 👉️ ['2', '4', '6', '8', '10'] list_of_integers = [int(x) for x in list_of_strings] print(list_of_integers) # 👉️ [2, 4, 6, 8, 10]

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

If you need to split a string on each digit, use a list comprehension.

main.py
my_str = '246810' list_of_ints = [int(x) for x in my_str] print(list_of_ints) # 👉️ [2, 4, 6, 8, 1, 0]

We iterate directly over the string and on each iteration, we convert the digit that is wrapped in a string to an integer.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.