Alan
Alan

Reputation: 559

Python Unnest Comma Separated Values in List

I have the follwoing list:

my_list = ['1', '7', '9, 11', '14']

How do I unnest comma-separated items like so:

new_list = ['1', '7', '9', '11', '14']

Upvotes: 0

Views: 128

Answers (2)

geckos
geckos

Reputation: 6279

Here it is

 new_list = [j.strip() for i in my_list for j in i.split(',')]

Upvotes: 2

Scott Hunter
Scott Hunter

Reputation: 49803

Here's one way:

new_list = []
for x in my_list:
    new_list += [a.strip() for a in x.split(",")]

Upvotes: 2

Related Questions