Learn Python 3: Project Thread Shed

Here is the situation. On the right hand side is the list is called “daily_transaction_split” and we wish to get rid of all the whitespaces by using strip( ). The code in red circle is what I did. but the output “transaction_clean” is the same as “daily_transaction_split”. I think it is because when I append lst to transaction_clean, lst remains the same wwhich means the previous step: each_string.strip( ) is not really update each lst. I think that is what things go wrong, but I just don’t know how to fix it.

If you trace through your loops you have the outside one using lst going through daily_transaction_split.
The inner one uses each_string to go through lst.
You then strip the whitespace from each_string and then
Then, once the middle loop is finished, you append lst to your new list transactions_clean.

Hopefully, that’s a few clues as to where I think you’re going wrong. Reply and I’ll elaborate if you’re still stuck

yep, that’s what went wrong. I think the issue is between line122 and line 124. It seems that line 123 doesn’t update each_string, thus, lst is not updated. Therefore, at line 124, when transaction_clean append lst, it just append the original one. That’s why transaction_clean is the same as daily_transaction_split

Hi, I actually know how to solve it. I just create another empty list and use this empty list to append each stripped each_string. Then, I use transaction_clean to append this empty list. Problem solved. Anyway, thank you for your help.

1 Like

Yeah, your main issue was on line 123 each_string.strip() wasn’t updating anything, so each loop it was just being discarded, and then on 124 you were updating your new list with whatever was in it in the first place.
Glad you’ve got a handle on it.