-1

I know there are a lot of similar questions but my use-case is unique. The following is my list ['apple,mango,fig']. What I want is to split the single element into multiple list elements based on ",". My final list should be like this ['apple','mango','fig']. As far as I know we can convert it into sub-list using split() method. But can I convert it into different elements of the same list? Is this possible?

eyllanesc
  • 221,139
  • 17
  • 121
  • 189
vishal
  • 1,304
  • 5
  • 20
  • 50
  • 1
    Yes, you can use `split()` here as well. If you had tried, you wouldn't want to ask a question. – Austin Feb 25 '20 at 07:08
  • Use ```split()``` for splitting list.\n **Please do try first, and share the code where you are facing problem, Just asking solution will not help.** – Shivam Seth Feb 25 '20 at 07:12

1 Answers1

1

If there is only one element in the list:

print(l[0].split(','))

If there are multiple elements in the list, use:

print([x for i in l for x in i.split(',')])
U12-Forward
  • 65,118
  • 12
  • 70
  • 89