-2

Here is my list

x = ['India,America,Australia,Japan']

How to convert above list into

x = ['India','America','Australia','Japan']

I tried it using strip and split method but it doesn't work.

Jainmiah
  • 439
  • 6
  • 16

1 Answers1

0

You can turn that list into the string and split in commas:

test = ['India,America,Australia,Japan']

result = "".join(test).split(",")

print(result)

Output is this:

['India', 'America', 'Australia', 'Japan']

or you can use regex library.

import re

x = "".join(['India,America,Australia,Japan'])

xText = re.compile(r"\w+")
mo = xText.findall(x)
print(mo)

The findall method looks for all the word characters and does not include comma. Finally it returns a list.

Output is this:

['India', 'America', 'Australia', 'Japan']