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
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
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
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

y.replace returns a value.

You have to assign it back

y = y.replace("[]", "o")
rafaelc
  • 52,436
  • 15
  • 51
  • 78