Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I process a list of strings where each string may be a comma-separated list of strings as well?

I want to convert this list

list = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']

to

new_list = ['orange','cherry','strawberry','cucumber','tomato','coconut','avocado','apple','blueberry','banana']

Is it possible to do that? Please help me!

like image 892
Jakhongir Alikhanov Avatar asked Dec 11 '22 00:12

Jakhongir Alikhanov


1 Answers

Join and split!

>>> lst = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']
>>> ','.join(lst).split(',')
['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana']
like image 74
timgeb Avatar answered Feb 02 '23 00:02

timgeb