Split a string into a list in Python


Borislav Hadzhiev

Last updated: Jun 24, 2022


Photo from Unsplash

Split a string into a list in Python #

Use the str.split() method to split a string into a list, e.g. my_list = my_str.split(','). The str.split() method takes a separator argument and splits the string on each occurrence of the provided separator.

# ✅ split string on each occurrence of a separator my_str = 'one,two,three,four' my_list = my_str.split(',') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] # -------------------- # ✅ split string into a list of characters my_str_2 = 'hello' my_list_2 = list(my_str_2) print(my_list_2) # 👉️ ['h', 'e', 'l', 'l', 'o']

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

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

The method takes the following 2 parameters:

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

If the separator is not found in the string, a list containing only 1 element is returned.

my_str = 'one' my_list = my_str.split(',') print(my_list) # 👉️ ['one']

We used a comma for the separator, but you can use any other character.

# 👇️ ['one', 'two', 'three'] print('one two three'.split(' ')) # 👇️ ['one', 'two', 'three'] print('one_two_three'.split('_')) # 👇️ ['one', 'two', 'three'] print('one.two.three'.split('.'))

The separator doesn't have to be a single character, it can be also be multiple characters.

my_str = 'one, two, three, four' my_list = my_str.split(', ') print(my_list) # 👉️ ['one', 'two', 'three', 'four']

If you need to split a string by whitespace, call the str.split() method without a separator.

my_str = 'a b \nc d \r\ne' my_list = my_str.split() print(my_list) # 👉️ ['a', 'b', 'c', 'd', 'e']
When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.

If your string starts with or ends with the specific separator, you would get empty string elements in the list.

my_str = '-one-two-three-four-' my_list = my_str.split('-') print(my_list) # 👉️ ['', 'one', 'two', 'three', 'four', '']

You can use the filter() function to remove any empty strings from the list.

my_str = '-one-two-three-four-' my_list = list(filter(None, my_str.split('-'))) print(my_list) # 👉️ ['one', 'two', 'three', 'four']

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

Note that the filter() function returns a filter object, so we have to use the list() class to convert the filter object to a list.

Sometimes you might have to split a string with X and Y delimiters.

One easy way to do this is to use the str.replace() method to replace all occurrences of X with Y and then split the string on Y.

my_str = 'one-two three-four' my_list = my_str.replace('-', ' ').split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four']

We replaced all occurrences of a hyphen with a space, and then split the string on the space.

We could've achieved the same result by replacing the the spaces in the string with hyphens and splitting on the hyphen.

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.