Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Split a List in Python by Comma

To split a comma-separated string into a Python list, use split(','). Here is how empty fields, spaces, maxsplit, rsplit, and multiple separators behave.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To split comma-separated text into a Python list, call split(',') on the string. A Python list is already a sequence of items, so the splitting step applies to the text that holds the commas, not to the list. The rest of this article covers the exact behavior of that call, the edge cases that produce surprising results, and the options for limiting or refining the split.

Start with the input: a string, not a list

Most people who search for this task have one of two situations in mind:

  • A string such as "red,green,blue" that needs to become ['red', 'green', 'blue']. This is what split(',') handles.
  • A list that already exists, which is already split into elements. There is nothing to split. If you need the elements joined into a comma-separated string, that is the reverse operation, ','.join(my_list).

The method only works on strings. If you call it on a value that is already a list, Python raises an AttributeError, because lists do not have a split method.

The basic split

Pass the comma as the separator argument to str.split():

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 comma is a literal delimiter. Python removes each comma and returns the pieces between them as strings. The method returns a new list and leaves the original string unchanged.

Edge cases that change the result

Empty fields are kept

When you give an explicit separator, Python keeps every field, including empty ones. Two adjacent commas produce an empty string, and a trailing comma produces an empty string at the end:

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

The method does not remove empty values for you. If your data can contain blank fields, decide what to do with them. You can filter them out with a list comprehension:

items = [part for part in 'a,,b,'.split(',') if part]
print(items)   # ['a', 'b']

Keep the empty strings when their position matters, for example in fixed-column records where a blank field means “no value.” Filter them when blanks are just formatting noise.

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

Spaces after commas stay in the output

The split happens exactly at the comma character. A space after a comma becomes part of the next element:

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

To remove that leading whitespace, strip each item:

items = [part.strip() for part in "red, green, blue".split(',')]
print(items)   # ['red', 'green', 'blue']

Omitting the separator splits on whitespace

If you call split() with no argument, Python does not split on commas. It splits on runs of whitespace, treats consecutive whitespace as one delimiter, and drops empty strings at the start and end:

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

This is a common source of confusion. If the goal is commas, always pass ',' explicitly.

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

Limit the number of splits

The optional maxsplit argument sets how many splits occur. The default, -1, means no limit. A positive value caps the number of splits, so the result contains at most maxsplit + 1 elements. The remainder of the string stays together in the last element:

line = "name,role,notes,with,commas"
print(line.split(',', 1))   # ['name', 'role,notes,with,commas']

Use this when only the first field or first few fields have a fixed meaning, and the rest of the line is free text that may itself contain commas.

Splitting from the right with rsplit()

rsplit() accepts the same arguments but applies the limit from the right. Compare the two results:

line = "name,role,notes,with,commas"
print(line.split(',', 1))    # ['name', 'role,notes,with,commas']
print(line.rsplit(',', 1))   # ['name,role,notes,with', 'commas']

Choose rsplit() when the fixed-meaning fields are at the end of the line, such as a trailing numeric value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Compare the choices at a glance

Call on "a,,b, c" Result When to use it
split(',') ['a', '', 'b', ' c'] Standard comma-separated text; empty fields matter
split(',', 1) ['a', ',b, c'] Only the first field is structured
rsplit(',', 1) ['a,', 'b, c'] Only the last field is structured
split() ['a,,b,', 'c'] Whitespace-separated text, not commas

When a plain comma split is not enough

A plain split(',') treats every comma as a delimiter. It does not understand quoting, so a value such as "Smith, John",42 breaks into three pieces. For CSV files that contain quoted fields, use the csv module from the standard library instead of hand-written splitting.

If the text uses more than one separator, such as commas and semicolons, pass a pattern to re.split() from the re module:

import re
print(re.split(r'[,;]', 'red,green;blue'))   # ['red', 'green', 'blue']

Note that re.split() treats the pattern as a regular expression, so it has its own rules for empty matches and special characters. Escape any characters that have meaning in regular expressions.

Reference for this behavior

The behavior described above comes from the str.split(sep=None, maxsplit=-1) entry in the Python Software Foundation’s Built-in Types documentation, which covers explicit-separator and whitespace splitting, empty fields, and the rsplit() difference. The reference used for this article is the Python 3.14 documentation as published in October 2026: https://docs.python.org/3/library/stdtypes.html?highlight=discard. Check the same section in your installed version if you are working on an older release.

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

“

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.