34

If I have a list of strings such as:

[("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]

What should I do in order to get rid of all the 8s in each string? I tried using strip or replace in a for loop but it doesn't work like it would in a normal string (that not in a list). Does anyone have a suggestion?

Bart
  • 19,692
  • 7
  • 68
  • 77
user1040563
  • 5,121
  • 9
  • 34
  • 36
  • 1
    In order to help you learn better, maybe you should show your code, so we can comment on why it didn’t work as you expected. – poke Nov 27 '11 at 00:17
  • 3
    As an aside, the parentheses are not needed in your list. Unless I'm missing something, it would be just as correct (and in this case, more intuitive I think) to have written lst = ["aaaa8","bb8","ccc8","dddddd8"] – Philip Uren Nov 27 '11 at 00:08

6 Answers6

77

Try this:

lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print([s.strip('8') for s in lst]) # remove the 8 from the string borders
print([s.replace('8', '') for s in lst]) # remove all the 8s 
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • he probably doesn't want to corrupt his data structure, what you get with your statement is list of strings, but had list of tuples of strings, shouldn't you replace s.strip('8') with (s.strip('8'),) ? – Jan Vorcak Nov 27 '11 at 00:08
  • 7
    @JanVorcak No, the original list is not a list of tuples, but a list of strings. As you wrote yourself, you create a one-tuple by `(x,)` but not just `(x)`. – poke Nov 27 '11 at 00:12
  • s.strip() in a list comprehension was just what I was looking for. thanks @jochen! – Amir Dec 21 '19 at 19:48
12

Beside using loop and for comprehension, you could also use map

lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylst = map(lambda each:each.strip("8"), lst)
print mylst
Tg.
  • 5,608
  • 7
  • 39
  • 52
  • 6
    Note that in Python 3, `map` returns a map object that needs to be converted to a list first. – poke Nov 27 '11 at 00:14
  • I don't have a chance to try out python 3 yet since I have a lot of library that depend on python2. Come to think about it, I guess I might migrate to 3 for new project. – Tg. Nov 27 '11 at 00:48
5

A faster way is to join the list, replace 8 and split the new string:

mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylist = ' '.join(mylist).replace('8','').split()
print mylist
Pyglouthon
  • 552
  • 4
  • 15
  • This won't work for cases where the word also has some '8' not in the end (ex. `"xxx8xx8"`) and the requirement is to strip the rightmost '8'. – Gino Mempin Jan 31 '20 at 07:38
1

Here's a short one-liner using regular expressions:

print [re.compile(r"8").sub("", m) for m in mylist]

If we separate the regex operations and improve the namings:

pattern = re.compile(r"8") # Create the regular expression to match
res = [pattern.sub("", match) for match in mylist] # Remove match on each element
print res
Walter R
  • 523
  • 1
  • 7
  • 10
1
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print mylist
j=0
for i in mylist:
    mylist[j]=i.rstrip("8")
    j+=1
print mylist
John
  • 106
  • 5
-2
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]

msg = filter(lambda x : x != "8", lst)

print msg

EDIT: For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8.

Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped.

To make this (kinda work?) with how the intent of the question was we could perform something similar to this

msg = filter(lambda x: x != "8", map(lambda y: list(y), lst))
  • I am not in an interpreter at the moment so of course mileage may vary, we may have to index so we do list(y[0]) would be the only modification to the above for this explanation purposes.

What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"].

This would result in a data type that looks like this

msg = [["a", "a", "a", "a"], ["b", "b"]...]

So finally to wrap that up we would have to map it to bring them all back into the same type roughly

msg = list(map(lambda q: ''.join(q), filter(lambda x: x != "8", map(lambda y: list(y[0]), lst))))

I would absolutely not recommend it, but if you were really wanting to play with map and filter, that would be how I think you could do it with a single line.

onaclov2000
  • 5,741
  • 9
  • 40
  • 54
  • For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8. Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped. To make this (kinda work?) with how the intent of the question was we could perform something similar to this ```` msg = filter(lambda x: x != "8", map(lambda y: list(y), lst)) ```` What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"]. – onaclov2000 Apr 27 '19 at 16:57