How can I delect the white space in a list?
I have:
text = [" Hello", " how are ","you "]
I want:
text = ["Hello","how are","you"]
CodePudding user response:
That's not the question you want to ask. You want to ask "how can I delete leading and trailing spaces?" You use .strip
for that.
text = [s.strip() for s in text]
CodePudding user response:
List = [" Hello", " how are ","you "]
List = [value.strip() for value in List]
print(List)
CodePudding user response:
You can use strip method on str and list comprehension, e.g.
text = [" Hello", " how are ","you "]
withoutWhiteSpace = [t.strip(' ') for t in text]
print(withoutWhiteSpace)
['Hello', 'how are', 'you']
CodePudding user response:
You can remove the white spaces at the beginning & at the end of each item in list by using below List comprehension procedure.
We are looping through each item in the list "text" & then using python string.strip() functionality to remove the whitespace.
text = [item.strip() for item in text]