For Loops Practice Question 20

In question 20 you append the list to add the length of the name to each row. I don’t understand what is happening. Why does appending the list ‘row’ change the output when we print ‘rows’? Why don’t we append the list ‘rows’ instead? I think maybe I’m not understanding fundamentally what’s happening. I thought row was a variable that we were separating out of rows. But do they remain linked together?

Here is the answer code:

\\for row in rows:
name = row[0]
row.append(len(name))

print (rows[0:4])\\

Hi @AlJens,

Welcome to the Community!

Here rows is a big list of lists, where each item (row) is a small two-item list containing a name and its corresponding frequency. When we iterate through these small lists (for row in rows:), we take each small list (row) and add (append) to its end the third item - the length of the name (name) in that small list (row). We do so for each of row in rows, at each iteration of our for-loop. At the end of the loop, the big list of lists rows will be modified in the following way: each of its small lists will now contain already 3 elements: name, frequency, and the length of the name.