I have a list filled with string objects. If the string object ends in a W
, I want to delete/remove the W
from the string. Also, in the only case that the string equals UW
I do not want to remove the W
.
I have tried this:
masterAbrevs = []
for subject in masterAbrevs:
if subject.endswith("W"):
subject = subject[:-1]
After printing masterAbrevs
it appears that my code is doing absolutely nothing. kindly help.
The problem is that you're never making the change to the element in the list; you're making the change to the variable holding the element returned from the list.
Try this instead:
masterAbrevs = ['ASW', 'AS', 'UW']
for i, e in enumerate(masterAbrevs):
if (e[-1] == 'W') and (e != 'UW'):
masterAbrevs[i] = masterAbrevs[i][:-1]
# results in ['AS', 'AS', 'UW']