Explanation of Code

I am on NumPy lesson 4/14. I just need some help understanding the logic of the following completed code:

# convert all values to floats
converted_taxi_list = []
for row in taxi_list:
    converted_row = []
    for item in row:
        converted_row.append(float(item))
    converted_taxi_list.append(converted_row)

I tried re-writing the following code to:
converted_taxi_list = []
converted_row = []
for row in taxi_list:
    for item in row:
        converted_row.append(float(item))
    converted_taxi_list.append(converted_row)

My question is pretty… neurotic, but why does my version of the code execute the same result when I have the “converted_taxi_list.append(converted_row)” line in-line with the “converted_row.append(float(item))” line or have it in line with the “for item in row” line? The former I understand because it is being executed as part of the of “for item in row” loop. But I don’t quite understand the Python logic of the latter.

Thanks

Hi @AWM007! Great inquisitiveness on your part - I had to think for a while before I realized why too!

This is the code from the solutions as you said:

converted_taxi_list = []
for row in taxi_list:
    converted_row = []
    for item in row:
        converted_row.append(float(item))
    converted_taxi_list.append(converted_row)

The converted_row = [ ] line being inside the first for loop means that the list represented by converted_row is reset to an empty list every single time that list repeats.

By bringing this line outside of the for loop, you made it so the converted_row list didn’t reset every time, and it just kept growing with each iteration. If it’s hard to visualize why, try and reproduce a similar problem with much smaller manually created lists! I’ve found this to be a helpful debugging habit.

Hi @AWM007

Welcome to our Dataquest Community.

In the first code:-

 converted_taxi_list = []    # Assign empty list to 'converted_taxi_list' variable.
for row in taxi_list:            # Loop over list of lists i.e. taxi_list contains list of taxies.
    converted_row = []       # Again assign empty list to 'converted_row' variable.
    for item in row:             # Loop over items of list i.e. loop on elements of the list of lists so, here list is the 'row' variable.
        converted_row.append(float(item))   # Adding element in 'converted_row' variable after converting its type in float.
    converted_taxi_list.append(converted_row)   # Here, adding 'converted_row' variable, which is a list in another list name as 'converted_taxi_list'.

So, this code converts the types of all the elements in a list of list i.e. list which is in the list taxi_list and then insert it into list converted_row and that list inserted into another list converted_taxi_list i.e. list of list.

And the second one:-

converted_taxi_list = []   # Assign empty list to 'converted_taxi_list' variable.
converted_row = []         # Again assign empty list to 'converted_row' variable.
for row in taxi_list:      # Loop over list of lists i.e. taxi_list contains list of taxies.
    for item in row:       # Loop over items of list i.e. loop on elements of the list of lists so, here list is the 'row' variable.
        converted_row.append(float(item))   # Adding element in 'converted_row' variable after converting its type in float.
    converted_taxi_list.append(converted_row)  # Here, adding 'converted_row' variable, which is a list in another list name as 'converted_taxi_list'.

What difference between them?

After the first looping in outer for loop of both the code converted_row list filled with float type elements.

But in the second loop, first code convert element of another list of taxi_list and insert into empty converted_row list and second code convert element of another list of taxi_list and insert into non-empty (contains the previous loop element) converted_row list.

I hope this helps :slightly_smiling_face: .

Thank you both for the replies. I should have modified my code to reflect my question instead of explaining it. My question is, why do I get the same output when I execute both these codes:

converted_taxi_list = []
converted_row = []
for row in taxi_list:
    for item in row:
        converted_row.append(float(item))
        converted_taxi_list.append(converted_row) 

and

converted_taxi_list = []
converted_row = []
for row in taxi_list:
    for item in row:
        converted_row.append(float(item))
    converted_taxi_list.append(converted_row)

For 1) I am sequentially telling the program to run those operations one after another. I understand this. Its for the 2) which I do not understand why converted_taxi_list.append(converted_row)is in-line with the converted_taxi_list.append(converted_row). Is it because it’s still part of the for item in row loop?

Hey @AWM007

You can’t get the same output from both the code.

In the first code, you are inserting converted_row in converted_taxi_list after loop on each element and in second code you are inserting after each loop on the list (not an element).

Let’s understand with example:-

taxi_list = [['1', '2'],['3', '4']]
# Output according to first code i.e. elements in converted_taxi_list after end of execution.
converted_taxi_list = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
Elements Outer For Loop Inner For Loop
[1] 1st ITR 1st ITR
[1, 2] 1st ITR 2nd ITR
[1, 2, 3] 2nd ITR 1st ITR
[1, 2, 3, 4] 2nd ITR 2nd ITR

Here, insertion after every iteration.

# Output according to second code i.e. elements in converted_taxi_list after end of execution.
converted_taxi_list = [[1, 2], [1, 2, 3, 4]]
Elements Outer For Loop Inner For Loop
[1, 2] 1st ITR 1st ITR
[1, 2] 1st ITR 2nd ITR
[1, 2, 3, 4] 2nd ITR 1st ITR
[1, 2, 3, 4] 2nd ITR 2nd ITR

Here, insertion after the end of Inner For Loop.

ITR stands for Iteration.

I hope this helps :slightly_smiling_face:

Thank you it does. I generally try to see if I can re-write the code in a different “logic” to better understand the concepts. I feel in this case there really isn’t.

1 Like