Convert a comma-separated string to a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

Convert a comma-separated string to a List in Python #

Use the str.split() method to convert a comma-separated string to a list, e.g. my_list = my_str.split(','). The str.split() method will split the string on each occurrence of a comma and will return a list containing the results.

main.py
my_str = 'one,two,three,four' # ✅ convert comma-separated string to list my_list = my_str.split(',') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] # ---------------------------------- my_str = '1,2,3,4' # ✅ convert comma-separated string to list of integers my_list = [int(item) if item.isdigit() else item for item in my_str.split(',')] print(my_list) # 👉️ [1, 2, 3, 4] # ---------------------------------- # 👇️ If your string has leading or trailing commas my_str = ',one,two,three,four,' my_list = [item for item in my_str.split(',') if item] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

We used the str.split() method to convert a comma-separated string to a list.

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

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

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)
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

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

main.py
print('one'.split(',')) # 👉️ ['one']

Make sure to adjust the separator if your string has a different delimiter.

main.py
# 👇️ comma and space delimiter my_str = 'one, two, three, four' my_list = my_str.split(', ') print(my_list) # 👉️ ['one', 'two', 'three', 'four']

To convert a comma-separated string into a list of integers:

  1. Use the str.split() method to split the string on each occurrence of a comma.
  2. Use a list comprehension to iterate over the list of strings.
  3. Use the int() class to convert each string to an integer.
main.py
my_str = '1,2,3,4' my_list = [int(item) if item.isdigit() else item for item in my_str.split(',')] print(my_list) # 👉️ [1, 2, 3, 4]

We used the str.split method to split the string on each occurrence of a comma and a list comprehension to iterate over the list.

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

On each iteration, we check if the current list item is a digit and use the int() class to convert the matching strings to integers.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

You don't have to check if you are sure that the string contains only comma-separated digits.

main.py
my_str = '1,2,3,4' my_list = [int(item) for item in my_str.split(',')] print(my_list) # 👉️ [1, 2, 3, 4]

If your string contains leading or trailing commas, use a list comprehension to exclude the empty string elements from the list.

main.py
my_str = ',one,two,three,four,' my_list = [item for item in my_str.split(',') if item] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

The string in the example has a leading and trailing comma, so splitting on a comma returns empty string elements.

main.py
my_str = ',one,two,three,four,' # 👇️ ['', 'one', 'two', 'three', 'four', ''] print(my_str.split(','))

You can use a list comprehension to exclude the empty strings from the list.

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.