0

Hello I am trying to remove the Character '+'

>>> a = ['eggs+', 'I don't want to remove this ', 'foo', 'spam+', 'bar+']
>>> a = [i[:-1] for i in a if i.ends with('+')]
>>> a
    ['eggs', 'spam', 'bar']
>>>

why are "I don't want to remove this" and the like getting removed and how do I just remove the '+' and leave every thing else like

>>>['eggs', 'I don't want to remove this ', 'foo', 'spam', 'bar']
Ina Plaksin
  • 157
  • 1
  • 3

1 Answers1

2

Try this:

a = ['eggs+', 'I dont want to remove this ', 'foo', 'spam+', 'bar+']
a = [i[:-1] if i.endswith('+') else i for i in a]
a

['eggs', 'I dont want to remove this ', 'foo', 'spam', 'bar']

You had some syntax issues, the if else must come before the iteration.

Kai Aeberli
  • 1,189
  • 8
  • 21