Home > Mobile >  how to replace certain pattern with nothing in lists
how to replace certain pattern with nothing in lists

Time:04-19

If a list looks like this:

['wo/od', 'axe/es']

and for replacing "/" with nothing "" I am using following code but its not working.

for lines in z:
    if lines == "/"
    lines.replace = ""

I am new to python Any help would be appreciated. I am also not allowed to hardcode it.

CodePudding user response:

Assuming that z is a reference to your list and bearing in mind that Python strings are immutable you could do this:

z = ['wo/od', 'axe/es']

z = [s.replace('/', '') for s in z]

CodePudding user response:

I can see you are using replace in wrong way it should be

z = ['wo/od', 'axe/es']

for lines in z:
    lines = lines.replace("/","")
    print(lines)

CodePudding user response:

Try this:

a = ['wo/od', 'axe/es']
for i, line in enumerate(a):
    a[i] = line.replace('/', '')
print(a)
  • Related