Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To split comma-separated text into a Python list, call split(',') on the string. The comma is passed as the separator, and Python returns a new list of the pieces between commas. For example, "red,green,blue".split(',') returns ['red', 'green', 'blue'].

The phrase “split a list by comma” can mean two different things, and the method you need depends on which one you have. If your data is a string such as "red,green,blue", use str.split(). If your data is already a list, such as ['red', 'green', 'blue'], there is nothing to split; you would instead join it back into text with ','.join(items), or process its elements directly.

Split a comma-separated string

The examples below follow the built-in types reference in the official Python 3.14 documentation for str.split(sep=None, maxsplit=-1). The method is available in every version of Python 3, and the behavior described here is the same across them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "red,green,blue"
items = text.split(',')
print(items)   # ['red', 'green', 'blue']

The separator is a literal string. Python does not interpret the comma as a pattern, and it does not understand quotes or brackets. Each comma ends one piece and the next piece begins.

Watch for spaces after commas

Splitting on a bare comma keeps any spaces that follow it. A string written as "red, green, blue" produces leading spaces in the later items:

"red, green, blue".split(',')
# ['red', ' green', ' blue']

You have two common fixes. You can use the separator that includes the space, which works when the spacing is consistent:

"red, green, blue".split(', ')
# ['red', 'green', 'blue']

Or you can strip each item after splitting, which also handles irregular spacing:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
[item.strip() for item in "red,  green ,blue".split(',')]
# ['red', 'green', 'blue']

Empty fields are kept

When you pass an explicit comma, split() keeps every empty field. Two commas in a row produce an empty string between them, and a trailing comma produces an empty string at the end:

'a,,b,'.split(',')
# ['a', '', 'b', '']

Python does not drop these for you. Decide what they should mean in your data before you proceed:

  • Preserve them when position matters, such as in CSV-like rows where an empty column is meaningful.
  • Remove them when they are only noise, using a filter: [item for item in text.split(',') if item].
  • Validate them when an empty value signals a data error, by checking for '' and raising an error or logging a warning.

If you combine filtering with stripping, filter after stripping. Otherwise an item that contains only spaces will survive the filter.

Limit the number of splits

The optional maxsplit argument sets how many splits occur. split(',', 1) makes at most one split from the left and returns at most two elements. This is useful when only the first field is a key and the rest of the line should stay together:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
'name,City, State'.split(',', 1)
# ['name', 'City, State']

rsplit() applies the same limit from the right. It is the better choice when the last field is the one you want to isolate:

'a,b,c'.rsplit(',', 1)
# ['a,b', 'c']
'a,b,c'.split(',', 1)
# ['a', 'b,c']

Without a maxsplit value, both methods split at every comma and return the same result.

Do not omit the separator

Calling split() with no arguments does not split on commas. It splits on runs of whitespace and discards empty strings at the edges. This is a frequent source of confusion:

'a, b c'.split()
# ['a,', 'b', 'c']

The comma stays attached to 'a,' because it is not whitespace. If your text is comma-separated, always pass ',' explicitly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a plain comma is not enough

Use split(',') when the separator is a single, known character and the values contain no commas inside them. For a string such as "red; green, blue", where several different separators can appear, the re module is the appropriate tool:

import re

re.split(r'[,;]s*', "red; green, blue")
# ['red', 'green', 'blue']

Plain splitting also cannot respect commas inside quoted fields, such as "Smith, John",42. That input is CSV-formatted data, and the standard library’s csv module handles quoting correctly. Use csv rather than split() for that kind of file.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Quick reference

Goal Code Result for 'a,,b,'
Split on every comma text.split(',') ['a', '', 'b', '']
Split on every comma, drop empties [x for x in text.split(',') if x] ['a', 'b']
One split from the left text.split(',', 1) ['a', ',b,']
One split from the right text.rsplit(',', 1) ['a,,b', '']
Split on whitespace instead text.split() ['a,,b,']

The official reference for these behaviors is the Python Software Foundation’s Built-in Types documentation, which covers str.split, rsplit, and the separator and whitespace rules described above.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.