How to convert comma-delimited string to list in Python?

ghz 3months ago ⋅ 31 views

Given a string that is a sequence of several values separated by a commma:

mStr = 'A,B,C,D,E' 

How do I convert the string to a list?

mList = ['A', 'B', 'C', 'D', 'E']

Answers

You can convert a string of values separated by commas into a list using the split() method. Here’s how you can do it:

mStr = 'A,B,C,D,E'
mList = mStr.split(',')

print(mList)

This will output:

['A', 'B', 'C', 'D', 'E']

Explanation:

  • The split(',') method takes the string and splits it at each comma, returning a list of the resulting substrings.