As a data scientist working with text data, I recently encountered a situation where I needed to process a dataset containing user-inputted tags stored as comma-separated strings. After researching different approaches, I found two efficient methods for handling this task. In this tutorial, I will explain how to convert a comma-separated string to a list in Python with examples.
Convert a Comma-Separated String to a List in Python
Let’s say you have a string of USA state names separated by commas, like this:
states_string = "California,New York,Texas,Florida,Illinois"Your goal is to convert this string into a Python list, where each state name becomes an individual element. The desired output would be:
states_list = ["California", "New York", "Texas", "Florida", "Illinois"]Read How to Remove HTML Tags from a String in Python?
Solution 1: Use the split() Method
The most simple way to convert a comma-separated string to a list is by using the built-in split() method in Python. The split() method in Python allows you to split a string into a list based on a specified delimiter.
Here’s an example:
states_string = "California,New York,Texas,Florida,Illinois"
states_list = states_string.split(",")
print(states_list)Output:
['California', 'New York', 'Texas', 'Florida', 'Illinois']I have executed the above example code and added the screenshot below.

The split() method takes the comma (",") as an argument, indicating that it should split the string whenever it encounters a comma. The resulting substrings are then stored as elements in the states_list.
Check out How to Remove Characters from a String in Python?
Solution 2: Use List Comprehension
Another concise way to convert a comma-separated string to a list is by using list comprehension. Python List comprehension allows you to create a new list based on an existing iterable, such as a string.
Here’s an example using list comprehension:
states_string = "California,New York,Texas,Florida,Illinois"
states_list = [state.strip() for state in states_string.split(",")]
print(states_list)Output:
['California', 'New York', 'Texas', 'Florida', 'Illinois']I have executed the above example code and added the screenshot below.

In this case, we split the states_string using split(",") and then iterate over each resulting substring using a for loop within the list comprehension. The strip() method is applied to each substring to remove any leading or trailing whitespace.
Check out How to Convert an Object to a String in Python?
Handle Edge Cases
When working with comma-separated strings, there are a few edge cases to consider:
- Empty String: If the input string is empty, splitting it will result in a list containing an empty string. You can handle this by checking if the string is empty before splitting it.
- Leading or Trailing Commas: If the string has leading or trailing commas, splitting it will result in empty strings at the beginning or end of the list. You can use the
strip()method to remove any leading or trailing whitespace, including commas. - Inconsistent Spacing: If the string has inconsistent spacing around the commas, the resulting list may contain substrings with leading or trailing whitespace. Again, using
strip()on each substring can help clean up the list of elements.
Read How to Format Decimal Places in Python Using f-Strings?
Conclusion
In this tutorial, I have explained how to convert a comma-separated string to a list in Python. I discussed two solutions to the given problem using the split() method, using list comprehension. I also discussed how to handle edge cases.
You may also like to read:
- How to Extract Numbers from a String in Python?
- How to Split a Long String into Multiple Lines in Python?
- How to Pad a String with Zeros in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.