tbysyl
tbysyl

Reputation: 1

Splitting a single item list at commas

MyList = ['a,b,c,d,e']

Is there any way to split a list (MyList) with a single item, 'a,b,c,d,e', at each comma so I end up with:

MyList = ['a','b','c','d','e']

Upvotes: 0

Views: 393

Answers (3)

balderman
balderman

Reputation: 23825

See below

lst_1 = ['a,b,c,d,e']
lst_2 = [x for x in lst_1[0] if x != ',']
print(lst_2)

output

['a', 'b', 'c', 'd', 'e']

Upvotes: 0

sspathare97
sspathare97

Reputation: 343

Use the split method on the string

MyList = MyList[0].split(',')

Upvotes: 2

Equinox
Equinox

Reputation: 6758

Split the first element.

MyList = ['a,b,c,d,e']
MyList = MyList[0].split(',')

Out:

['a', 'b', 'c', 'd', 'e']

Upvotes: 3

Related Questions