0

I am reading data in from a text file so each row is a list of strings, and all those lists are in a data list. So my lists look like:

data = [row1, row2, etc.]
row1 = [str1, str2, etc.]

I am trying to remove any $ or % signs that appear in the strings in a row list. I have checked that if I try and do this for one individual element, say the second row and the fourth element has a "%", so:

data[1][3] = data[1][3].replace("%","")

This will properly remove it, but when I try and use a nested for loop to do it all:

for row in data:
    for x in row:
        x = x.replace("%","")
        x = x.replace("$","")

This doesn't remove any of the % or $ signs, I have tried just doing it without the second replace to see if it would at least remove the % signs, but even that didn't work.

Any ideas why this wouldn't work, or how I could do this?
Thanks in advance for any help!

4
  • 2
    Please do not use str as a name for a variable in Python. It is also a function! Commented Sep 25, 2016 at 0:45
  • 4
    for-in doesn't affect the actual list because the element is stored/copied into a variable, it's not a direct reference. Use enumerate and access list. Commented Sep 25, 2016 at 0:46
  • I will update, I don't actually have it that way in my code I just wrote it that way for clarity. Commented Sep 25, 2016 at 0:46
  • @CRT I also editted to show the second issue with code and a simple remedy using enumerate(). Commented Sep 25, 2016 at 0:53

3 Answers 3

3

The problem is that your str variable is shadowing the builtin Python str variable. That fix is easy. Just use another variable name.

The second problem is that the replaced string isn't being replaced in the row list itself. The fix is to save the replaced string back into the row list. For that, you can use enumerate() to give you both the value and its position in the row:

for row in data:
    for i, x in enumerate(row):
        x = x.replace("%","")
        x = x.replace("$","")
        row[i] = x
Sign up to request clarification or add additional context in comments.

5 Comments

Is that the only problem?
Use of the name str is a code smell but it wouldn't stop it from working.
I just updated it, I don't actually have it as str in my code, I was writing it that way here to try and make it clear what I was looping through.
@AndrewL. There were two problems. I've commented on both.
Thanks, I am still new to python, so I need to look into enumerate more, but that did solve the problem.
0

You are assigning a new value to the name x but that does not change the contents of row or data. After changing x, you need to assign row[j] = x or data[i][j] = x for the appropriate column index j (and row index i). See also python 3: lists dont change their values

Comments

0

Also in your in this case you can use list comprehension

data = [[item.replace("%", "").replace("$", "0") for item in row] for row in data]

2 Comments

What are list expressions? do you mean list comprehension?
Sure, it's my mistake. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.