0

I have tried to use .replace in python to replace an empty list in a string but it is not working. Could anyone please tell me how?

x = ['check-[]|man', 'check-[]|king']

for y in x:
    if "[]" in y:
        y.replace("[]", "o")
        print(y)

The results gave me this despite using .replace:

check-[]|man
check-[]|king
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Joe
  • 71
  • 9

2 Answers2

1

You need to assign i back to variable y:

x = ['check-[]|man', 'check-[]|king']

for y in x:
    if "[]" in y:
        y=y.replace("[]", "o")
        print(y)

Output:

check-o|man
check-o|king
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

y.replace returns a value.

You have to assign it back

y = y.replace("[]", "o")
rafaelc
  • 57,686
  • 15
  • 58
  • 82