Reputation: 29
I have a list like ;
list1 = ['ex1_a','ex2_b','ex3_b']
I want to split it like this to the new list ;
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("_"))
Upvotes: 1
Views: 51
Reputation: 195603
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]
Upvotes: 1