Python Thread Shed (list items duplicated)

I don’t know what I did wrong, but the transactions duplicated like Tribbles in Star Trek TOS.

daily_sales_replaced = daily_sales.replace(';,;', ';')

daily_transactions = daily_sales_replaced.split(',')
#print(daily_transactions)

daily_transactions_split = []
for line in daily_transactions:
  daily_transactions_split.append(line.split(';'))
#print(daily_transactions_split)

transactions_clean = []

for line in daily_transactions_split:
  transaction_clean = []
  for element in line:
    transaction_clean.append(element.replace('\n', '').strip())
    transactions_clean.append(transaction_clean)
#print(transactions_clean)
#print(transaction_clean)

I’m guessing that you need to double check the indentation on your second for loop in your function. But, it’s difficult to tell b/c your code isn’t formatted. Please format your code by using “</>”.

Alternatively, you can also use search here in the forums, as some other posts on this very issue might give you a hint as to what the issue is. But, like I said, move the indentation around in your function.

Ok, thanks for letting me know. It’s formatted now.

did you move the indentation around here in the 2nd for loop? When you get stuck like this, don’t be afraid to move the code around to see what happens or what errors you receive.
Specifically,

Summary
    transactions_clean.append(transaction_clean)

The way you have the code written now, you’re cleaning, appending, and appending again.

I’ve tried messing around with indentation, but it still didn’t work.

How should I correct this?

transactions_clean.append(transaction_clean)

I followed along with the video, and it’s stil not working.

It’s not very clear in the video. It’s this part:

for line in daily_transactions_split:
  transaction_clean = []
  for element in line:
    transaction_clean.append(element.replace('\n', '').strip())
    transactions_clean.append(transaction_clean)

In the inner for loop, transactions_clean.append() needs correcting.
Move that and see what transpires.

1 Like

Ok, finally got it. The second append statement needed to be outside of the second for loop. Thanks!

1 Like

Yes, b/c the way you had it initially, you were cleaning, appending, and appending again.

This was really tricky for me too. Step. 8 Hints ( This will require two for loops. One to iterate through the outer list, daily_transactions_split, and one to iterate through each of the transactions and perform the .strip() on the data points.) so I ended up writing this particular section as follows:

transactions_clean =
for transaction in daily_transactions_split:
clean_transaction =
for data_point in transaction:
clean_transaction.append(data_point.strip())
transactions_clean.append(clean_transaction)
print(transactions_clean)

Hope this help