Python 3 - Trouble understanding lists within nested loops

Hello,

This code block is from the Thread Shed project of the Learn Python 3 course, in the Strings lesson. I shortened the daily_transactions_split list to the first 3 items for readability.

I’m having trouble intuitively understanding what is happening with the lists in the nested for loops below. This is making it difficult to re-write code involving the same/similar concepts without lots of trial and error. Among my questions are:

  1. Why does transaction_clean have to go after the first for loop? Why can’t it go before it (outside the for loops entirely as transactions_clean is?
  2. Why does transactions_clean.append(transaction_clean) have to go in the first for loop but cannot go in the second?
  3. Why does printing transaction_clean after the for loops cause only the last item in the list to be printed?
  4. What is happening on the computational level when a for loop is executed within another for loop?

I guess I’m trying to have these concepts explained in a different way that will hopefully help make them click.

Thank you!

daily_transactions_split = [['Edith Mcbride   ', '$1.21   ', '   white ', ' \n09/15/17   '], ['Herbert Tran   ', '   $7.29', ' \nwhite&blue', '   09/15/17 '], ['Paul Clarke ', '$12.52 \n', '   white&blue ', ' 09/15/17 ']]
print(daily_transactions_split, '\n')

transactions_clean = []
for transaction in daily_transactions_split:
    transaction_clean = []
    for data_point in transaction:
        transaction_clean.append(data_point.strip())
    transactions_clean.append(transaction_clean)

print(transactions_clean, '\n')

print(transaction_clean)

Link to project: https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

Because that list gets cleared and refilled on each iteration of the outer loop. It’s contents are spilled into the transactions_clean list at the end of each inner loop cycle.

Because that is the cumulative list we’re going to need when the outer loop is completed.

Because that is all it contains. The other list is the complete store of all the lists.

The outer loop will iterate so many times. Each time the inner loop iterates completely, usually given a value or values from the outer loop.

Let’s say there are ten items in the daily_transactions_split list. Those ten items have four items each, say. That means the outer loop will iterate ten times, and the inner loop will iterate four times in each, meaning the code inside that loop will execute forty times in total.

3 Likes

Thanks! One more question:

When I put transactions_clean.append(transaction_clean) above the inner for loop (but still within the outer loop), I get the same result. Isn’t the code executed from top to bottom within for loops? If so then shouldn’t nothing ever be appended to transactions_clean because that line (transactions_clean.append(transaction_clean) would be executed in each iteration before anything gets appended to transaction_clean in the inner for loop…?

Try refreshing the page and see what happens.

I restarted the kernel in Jupyter Notebook and still get the same output as before. This is the new code block I’m running:

daily_transactions_split = [['Edith Mcbride   ', '$1.21   ', '   white ', ' \n09/15/17   '], ['Herbert Tran   ', '   $7.29', ' \nwhite&blue', '   09/15/17 '], ['Paul Clarke ', '$12.52 \n', '   white&blue ', ' 09/15/17 ']]

transactions_clean = []
for transaction in daily_transactions_split:
    transaction_clean = []
    transactions_clean.append(transaction_clean)
    for data_point in transaction:
        transaction_clean.append(data_point.strip())

print(transactions_clean)

Output:

[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17'], ['Paul Clarke', '$12.52', 'white&blue', '09/15/17']] 

The only way to explain that is with a reference. Even though the list has been appended, and it is empty at the time, the reference to it still exists so when that list is appended, Python knows where it is.

1 Like