Thread Shed project Step 8

In the 8th step of the project Thead Shed I wrote the Following code:

transactions_clean = []

for transaction in daily_transactions_split:
  for datapoint in transaction:

    transactions_clean.append(datapoint.replace("\n", "").strip(" "))

I was wondering why my code is wrong and why do we need to create a list in the loop and use that list to append the clean datapoints and than append that list to the final list namely transactions_split.

Why Cant we directly append to that list in the first place and not define the list inside the loop.

Any Help would be greatly appreciated.

Hi,

As a matter of practice, in the beginning I would use printing statements inside the loop to see how things are being iterated. This can be useful when you have a lot of complicated operations going on.

Think about the state of your data before you start your loops, think about what operations you want to do to that data. The replace and strip methods are great but they are in the end, also abstractions (and can be written explicitly).

The more you practice clarifying these steps, the quicker you’ll be able to produce solutions and troubleshoot bugs quickly in your head. Even better, if you have time, I would map out on paper what you think is happening in your code (even before putting print statements), that way you don’t use print as a crutch.

1 Like

Thank you for your prompt reply.

But I am still not able to quite get why we cant directly append to the transactions_clean list and why we have to create a list inside the loop and append to it and after that append to the final list.

Hi,

There’s often more than one way to do things. At times there are ideal ways, stylistically consistent ways, and pedagogical (learning ways). I think the better point is to compare what the results of your code are versus the way you saw the answer solution do it.

It’s not clear from what you write whether your code is wrong (as in it doesn’t do the desired task) or your code being different from the provided solution.

Also, post a link to the exercise or paste what the solution suggests instead for feedback as to why that particular solution was presented.