List and for loop understanding

Hey all,

just started my python journey recently and had a quick “understanding” question.

from csv import reader
opened_file = open('dq_unisex_names.csv')
read_file = reader(opened_file)
data = list(read_file)

headers = data[0]
rows = data[1:]

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

from the above, am I right in saying that:

The for loop allows me to repeat the following process for each element within ‘rows’. in other words, each element within rows has an action (defined in the for body) performed on it?

in the above code, the ‘row’ in for row in rows: is a variable that i just made referencing one element? or is it reference one list within the list of lists? what is the difference between an element and a list in this case?

and name=row[0] is therefore referencing the first element i.e. index number 0?

…final question
as it is a loop, does that mean

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


the above is repeated throughout each element? so the next part of the sequence would be

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

and so on, until it reaches the final index number?

thank you for your patience :slight_smile:

The general question that would answer all queries here (and for most other problems) would be:
How do I see what any variable represents?

To study variables, we want to know

  1. Value
  2. type()
  3. id()
  4. Scope

3, 4 are related and less important because usually if we know 1,2 it’s enough to find our way around what is what.

To get those information, there are a few methods.

  1. print()
  2. Stepping through IDE debuggers
  3. Logging (when you are more experienced and doing production work)

Try to insert print statements between every data transformation to see what’s going on (one day when you find this tedious or impossible, you will automatically move to IDE debugger).

Without prints, you are coding blind.

Same thing. Difficulty is knowing what 1 element of a loop represents. It is different if you loop through different objects. Some of these objects are not python built-ins but custom classes created by others (when you use libraries you’ll see). Printing is the most direct way to grasp what’s going on.

Again what is an element? If I have a list of lists (2 Dimensional object), list_of_list[0][1] is this getting element 0 or 1? What happens if i do list_of_list[0][1][0]?

When doing experiments, you don’t have use the csv reader anymore, just simulate your own rows and start printing from there using various indexing patterns.

No the for loop only updates the variables defined right after for in each iteration. In the code, The 0 in row[0] is hardcoded, it does not vary. Only the row in for row in rows points to a different element on each iteration. Then you observe which parts in the for loop body uses row to infer what changes.

For your info, list() and [] do different things. list() will loop through its iterable argument, so there’s 1 level of unpacking, while [] does not unpack.

1 Like