How to Split a List in Python by Comma

Recently, while working on a data-cleaning project, I needed to split a long list of values separated by commas into individual items.

At first, I thought this would be tricky, but Python makes it surprisingly simple. With over 10 years of experience writing Python code, I’ve used this technique countless times for real-world projects.

In this tutorial, I’ll show you different ways to split a list in Python by comma. Each method is practical, beginner-friendly, and something I personally use in my day-to-day coding.

Methods to Split a List in Python by Comma

When working with data in the USA, it’s common to receive information as comma-separated values (CSV). For example, think of sales data, customer emails, or even addresses.

Instead of manually separating them, Python allows us to automate this process. Splitting by a comma is one of the most common text-processing tasks you’ll face.

1 – Use the split() Method in Python

The easiest way to split a string into a list by a comma is to use Python’s built-in split() method. This method is simple, fast, and works in almost every scenario where your data is plain text.

data = "New York,Los Angeles,Chicago,Houston,Phoenix"
city_list = data.split(",")

print(city_list)

When I run this code, Python returns:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can refer to the screenshot below to see the output.

python split string by comma

This method is perfect when you have a clean string and you just want to break it into a list.

2 – Handle Extra Spaces While Splitting

Sometimes, when you split a string by comma, you may notice unwanted spaces around the items. Python makes it easy to clean this up using list comprehension and the strip() method.

data = "New York, Los Angeles, Chicago, Houston, Phoenix"
city_list = [city.strip() for city in data.split(",")]

print(city_list)

The output is:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can refer to the screenshot below to see the output.

python print list with commas

I use this approach often when working with messy datasets in Python, especially when data comes from CSV files.

3 – Split a Python List of Strings by Comma

Sometimes, you already have a list in Python, but each element contains multiple comma-separated values.

In this case, you can loop through the list and split each element individually.

data = ["Apple,Orange", "Banana,Grapes", "Mango,Pineapple"]
result = [item.split(",") for item in data]

print(result)

The result is:

[['Apple', 'Orange'], ['Banana', 'Grapes'], ['Mango', 'Pineapple']]

You can refer to the screenshot below to see the output.

python comma separated string to list

This is useful when parsing survey responses or product categories stored in a list.

4 – Use Python’s csv Module

When working with real CSV files in the USA, I prefer using Python’s built-in csv module. The csv module automatically handles commas, quotes, and even escape characters.

import csv
from io import StringIO

data = "New York,Los Angeles,Chicago,Houston,Phoenix"
f = StringIO(data)
reader = csv.reader(f, delimiter=",")

for row in reader:
    print(row)

The output is:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can refer to the screenshot below to see the output.

python list to string with commas

This method is more reliable than split() when dealing with complex or real-world CSV data.

5 – Split by Comma with Python Regular Expressions

When data is inconsistent, I often rely on Python’s re module. For example, sometimes commas are mixed with spaces, tabs, or multiple delimiters. Regular expressions give me more control.

import re

data = "New York, Los Angeles ,Chicago ,  Houston,Phoenix"
city_list = re.split(r'\s*,\s*', data)

print(city_list)

The output is:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can refer to the screenshot below to see the output.

python split by comma

This approach is powerful when you deal with unstructured text in Python.

6 – Convert a Comma-Separated String into Integers

In many Python projects, comma-separated values are numbers, not text. For example, sales data might come in as “100,200,300,400”. You can split and convert them into integers in one step.

data = "100,200,300,400,500"
numbers = [int(num) for num in data.split(",")]

print(numbers)

The output is:

[100, 200, 300, 400, 500]

This is especially useful in finance or analytics projects where calculations are required.

7 – Split Multi-Line Comma-Separated Data

Sometimes, you’ll receive data with multiple lines, each containing comma-separated values. Python makes it easy to process this kind of input using a loop.

data = """New York,Los Angeles,Chicago
Houston,Phoenix,Philadelphia
San Antonio,San Diego,Dallas"""

lines = data.split("\n")
city_list = [line.split(",") for line in lines]

print(city_list)

The result is:

[['New York', 'Los Angeles', 'Chicago'],
 ['Houston', 'Phoenix', 'Philadelphia'],
 ['San Antonio', 'San Diego', 'Dallas']]

This is a common scenario when parsing logs or exported reports in Python.

8 – Use Python Pandas for Large-Scale Data

For large datasets, I often use the Pandas library. Pandas can split comma-separated values quickly and efficiently.

import pandas as pd

data = {"Cities": ["New York,Los Angeles,Chicago", "Houston,Phoenix,Philadelphia"]}
df = pd.DataFrame(data)

df["Cities_Split"] = df["Cities"].str.split(",")

print(df)

The output is:

                       Cities                          Cities_Split
0     New York,Los Angeles,Chicago     [New York, Los Angeles, Chicago]
1  Houston,Phoenix,Philadelphia     [Houston, Phoenix, Philadelphia]

This method is best when you’re dealing with structured data in Python and want to keep results in a DataFrame.

In this tutorial, I showed you multiple methods: using split(), handling spaces, working with lists, using csv, regex, integers, multi-line data, and Pandas.

Each method has its place. For clean strings, split() works great. For messy data, regex or csv is better. And for large datasets, Pandas is my go-to choice.

You may also read other Python articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.