Thread Shed

Hi all, working on the Thread Shed Project -https://www.codecademy.com/courses/learn-python-3/projects/thread-shed at Step 8.

It looks like each data item has inconsistent whitespace around it. First, define an empty list transactions_clean .

Now, Iterate through daily_transactions_split and for each transaction iterate through the different data points and strip off any whitespace.

Add each of these cleaned up transactions to the new list transactions_clean .

So I’ve since figured out the answer, but I’m more wondering why… The solution was -

transactions_clean =

for lists in daily_transactions_split:

 strings = []

 for string in lists:

   strings.append(string.strip())

 transactions_clean.append(strings)

print(transactions_clean)

But originally I had written as:

transactions_clean =

strings = []

for lists in daily_transactions_split:

 for string in lists:

    strings.append(string.strip())

 transactions_clean.append(strings)

print(transactions_clean)

This resulted in one massive list containing all the data cleaned up. Rather than a huge list with sublists, which is required for the step 10-12 to work. I was stuck on this for awhile before moving the ‘strings’ list inside the loop. My question is why does it work this way? Why does the creation of an empty list inside or outside of the loop, matter to the formatting of it as a sublist? I thought the strings.append followed by the transactions_clean.append would format it as needed, not the location of the list prior to appending. Can anyone clarify why?

Note the difference in types.
strings in one case is a list of transaction details per iteration. The key is that it resets itself upon every iteration.
In the second case, strings is a list that grows per iteration. You still append the same number of times, but every time you append strings, you include all the information you had the in the previous case.

It may help if you try to do this with a small toy model, rather than the huge behemoth that’s the string you’re given.