If I have a list of strings and want to eliminate leading and trailing whitespaces from it, how can I use .strip() effectively to accomplish this?
Here is my code (python 2.7):
for item in myList:
item = item.strip()
print item
for item in myList:
print item
The changes don't preserve from one iteration to the next. I tried using map as suggested here (https://stackoverflow.com/a/7984192) but it did not work for me. Please help.
Note, this question is useful:
- An answer does not exist already
- It covers a mistake someone new to programming / python might make
- Its title covers search cases both general (how to update values in a list) and specific (how to do this with .strip()).
- It addresses previous work, in particular the map solution, which would not work for me.