Hello my friend;
Strings in python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.
Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.
You can instead use str.translate: (https://docs.python.org/2/library/stdtypes.html#str.translate)
line = line.translate(None, '!@#$')
which only works on Python 2.6 and newer Python 2.x versions * —
or regular expression replacement with re.sub (https://docs.python.org/2/library/re.html#re.sub)
import re
line = re.sub('[!@#$]', '', line)
Here is an example:
s = "this is a string"
l = list(s) # convert to list
l[1] = "" # "delete" letter h (the item actually still exists but is empty)
l[1:2] = [] # really delete letter h (the item is actually removed from the list)
del(l[1]) # another way to delete it
p = l.index("a") # find position of the letter "a"
del(l[p]) # delete it
s = "".join(l) # convert back to string
Timing comparison
def findreplace(m_string, char):
m_string = list(m_string)
for k in m_string:
if k == char:
del(m_string[m_string.index(k)])
return "".join(m_string)
def replace(m_string, char):
return m_string.replace("i", "")
def translate(m_string, char):
return m_string.translate(None, "i")
from timeit import timeit
print timeit("findreplace('it is icy','i')", "from __main__ import findreplace")
print timeit("replace('it is icy','i')", "from __main__ import replace")
print timeit("translate('it is icy','i')", "from __main__ import translate")
Result
1.64474582672
0.29278588295
0.311302900314
str.replace and str.translate methods are 8 and 5 times faster.
Regards.
Try this code: name = [i.replace(",", "") for i in name]