1

I have the list:

mylist = ["8 - 9-", "7 - 6-", "4-"]

How would I remove the last "-" from each element in mylist?

so that the output is

mylist = ["8 - 9", "7 - 6", "4"]

edit: Originally I had a space before the "-" but that should not have been there. Apologies.

PiccolMan
  • 4,854
  • 12
  • 35
  • 53

3 Answers3

3

rstrip and a list comprehension will do the job:

mylist = [s.rstrip('-') for s in mylist]

Strictly speaking, this doesn't remove "the last '-' from each element", but rather removes any trailing dashes. But given your sample data, it does what you'd like.

For example:

>>> mylist = ["8 - 9-", "7 - 6-", "4-"]
>>> mylist = [s.rstrip('-') for s in mylist]
>>> print(mylist)
['8 - 9', '7 - 6', '4']
jme
  • 19,895
  • 6
  • 41
  • 39
  • This will fail in case of `-4- ` – vks Aug 13 '15 at 18:05
  • @vks Sure, but the sample input has no case where a `-` is followed by a space. I'm guessing that my answer is what the OP was *really* asking for -- otherwise, he's free to use a more complicated solution. – jme Aug 13 '15 at 19:52
  • Your code should work for the case that I added above, and thanks. However, I screwed up when asking my question because I should not have added a space after the "-". – PiccolMan Aug 13 '15 at 20:26
  • @PiccolMan In that case, the code I posted still works. – jme Aug 13 '15 at 20:33
  • @jme for some reason this isn't working for me. Do I have to load a module? Also, I am using python 3. I'll accept your answer since this is a problem on my end, and you were the first to provide help. Thanks. – PiccolMan Aug 14 '15 at 00:50
  • @PiccolMan When you say "this isn't working", what do you mean? – jme Aug 14 '15 at 01:06
  • It does not do anything to my list, I'll probably figure out the problem don't worry. – PiccolMan Aug 14 '15 at 01:13
  • @PiccolMan Make sure you're reassigning the result of the list comprehension to `mylist`. I've added an example. – jme Aug 14 '15 at 01:20
  • I found the error. There is a random number of spaces after each string. So for example, the first element would be something like " 8 - 9- insert spaces here ". Is there a way to remove those spaces? I'll try using a list compression for the spaces. – PiccolMan Aug 14 '15 at 02:39
  • @PiccolMan Yep. Change `s.rstrip('-')` to `s.rstrip('- ')`. That will remove any amount of spaces and dashes from the string. – jme Aug 14 '15 at 02:41
2

Using re would be more suitable as it can handle cases where there are spaces after -.It would remove them as well.

import re
print [re.sub(r"\s*-\s*$","",i)  for i in ["8 - 9 -", "7 - 6 -", "4 -"]]

Output:['8 - 9', '7 - 6', '4']

vks
  • 67,027
  • 10
  • 91
  • 124
  • [This](http://stackoverflow.com/questions/2556108/how-to-replace-the-last-occurence-of-an-expression-in-a-string) would be good as well. – TigerhawkT3 Aug 13 '15 at 18:08
  • @TigerhawkT3 the problem with it is it will remove `-` from `-4` as well – vks Aug 13 '15 at 18:14
  • I guess that may or may not be desired, as the question's sample input doesn't cover these cases. – TigerhawkT3 Aug 13 '15 at 18:15
2

If you want a strict "remove the last dash in each string," you can do this:

>>> mylist = ["8 - 9 -", "7 - 6 -", "4 -", "4 - ", "4 - 4"]
>>> [''.join(item.rsplit('-', maxsplit=1)) for item in mylist]
['8 - 9 ', '7 - 6 ', '4 ', '4  ', '4  4']

Note how the last dash is removed even if there is a number after it.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97