Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting Strings In List

I have a list like ;

list1 = [‘ex1_a’,’ex2_b’,’ex3_b’]

I want to split it like this to the new list ;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

newlist = [‘ex1 a’, ‘ex2 b’, ‘ex3 b’]

I try to do it like this but it didn’t work. How can I do this?

for x in list1:
             newlist = " ".join(x.split("_"))

>Solution :

You can use str.replace instead str.split/str.join:

list1 = ["ex1_a", "ex2_b", "ex3_b"]

newlist = [v.replace("_", " ") for v in list1]
print(newlist)

Prints:

['ex1 a', 'ex2 b', 'ex3 b']

If you want to use str.split:

newlist = [" ".join(v.split("_")) for v in list1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading