Hey community!
I’m working on the objective above in the learn python 3 course. I’m a little stuck on tasks 8 and 9. I’ve looked at the developer walk-through video on YouTube but not sure what I’m doing wrong. When I print my transactions_clean list I’m noticing that each sublist is being duplicated or rather appearing 3 times. My code is below. From what I can tell I’ve done the for loop as the developer has here: Python Project Thread Shed - YouTube
# Start coding below!
daily_sales_replaced = daily_sales.replace(';,;', ';')
daily_transactions = daily_sales_replaced.split(',')
#print(daily_transactions)
daily_transactions_split = []
for transaction in daily_transactions:
daily_transactions_split.append(transaction.split(';'))
#print(daily_transactions_split)
transactions_clean = []
for transaction in daily_transactions_split:
transaction_clean = []
for item in transaction:
transaction_clean.append(item.replace("\n", "").strip(" "))
transactions_clean.append(transaction_clean)
print(transactions_clean)
I haven’t watched the video, but if you are getting three of something appended to a list that you only wanted one of, the most likely reason is that the code that appends the thing is inside a loop. Is your code that appends to transactions_clean inside a loop that it shouldn’t be inside of?
Edit: Just had a peek at the video. The indentation isn’t actually as it appears in the video. The code editor window is too narrow, so a line of code is moved down, and the indentation appears to be what it really isn’t. Never mind the video. Think about what needs to happen.
I did notice he did some sort of other indent but looking at the arrow in the for loop it’s still pointing down in the second for loop which from my understanding suggests we’re still working in that indent. When I remove that action from the for loop I get an error and I’m not sure it makes sense since the following
That line does belong inside the nested for loop. The line that appends to transactions_clean being inside the nested for loop is why you are getting the desired result appended 3 times instead of once.
When I tried un-nesting I got errors:
Inside the first for loop
File "script.py", line 127
transaction_clean.append(item.replace("\n", "").strip(" "))
^
IndentationError: expected an indented block
Outside both for loops
File "script.py", line 127
transaction_clean.append(item.replace("\n", "").strip(" "))
^
IndentationError: expected an indented block
The developer doesn’t mention how he’s indenting
I did make this quick loom in case you wanted to see how it’s structured and maybe it’s explained better: Loom | Free Screen & Video Recording Software
transactions_clean = []
for transaction in daily_transactions_split:
transaction_clean = []
for item in transaction:
transaction_clean.append(item.replace("\n", "").strip(" ")) #executed once for every item in transaction
transactions_clean.append(transaction_clean) #executed once for every transaction in daily_treansactions_split
print(transactions_clean)
OOOOH thank you! I’m sorry, I realized you said " That line does belong inside the nested for loop. The line that appends to transactions_clean…" did not register that you were referring to
transactions_clean.append(transaction_clean)
but that does make total sense. Thanks for bearing with me there