# Numpy intro - ndarray for loop

I do not understand why the list named converted_row was placed inside the for loop rather than outside.
I think this would create a new list for every loop iteration and am not sure of the reason why. Why was it not declared outside of the for loop like converted_taxi_list was?

``````   converted_taxi_list = []
for row in taxi_list:
converted_row = []
``````

https://app.dataquest.io/m/289/introduction-to-numpy/4/nyc-taxi-airport-data

1. Add a line of code using the `numpy.array()` constructor to convert the `converted_taxi_list` variable to a NumPy ndarray.
2. Assign the result to the variable name `taxi` .
``````import csv
import numpy as np

# import nyc_taxi.csv as a list of lists
f = open("nyc_taxis.csv", "r")

taxi_list = taxi_list[1:]

# 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)

# start writing your code below this comment
taxi = np.array(converted_taxi_list)``````

Hello @jamesberentsen,

Yes, placing the `converted_row` list inside the `for` loop would create a new list for every iteration. That is what we need to do to make the block of code work as intended.

Let me explain:

The aim is to convert all values in the list of lists `taxi_list` to floats, so for each row/ list in `taxi_list`, you convert each item in that row to `float` and assign each converted item to a new/empty list called `converted_row`. After which you then append the converted row to the new list of lists `converted_taxi_list`. So after the `converted_row` list has been appended to the list of lists, it has to become empty for the next row, so as to contain the new converted items of the new row.

So the steps involved are:

1. Create a new list of list `converted_taxi_list` to contain all the converted rows
2. Take a row in the list of lists
3. create a new list `converted_row` to store the converted_items of that row
4. for each item in the row from step2, convert it to `float` and append it to the new list created in step 3.
5. append the row in step 3 to the new list of lists `converted_taxi_list`.
6. Take the next row and repeat steps 3 - 5.
7. Take the next row and repeat steps 3-5, till all rows are converted and appended to the new list of lists.

So after step2, you always need a new/empty list to store all the converted_items in the row/list you took in step2.

I tried to explain in the simplest terms possible, I hope this makes sense to you.

``````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)
``````
1 Like

Many thanks,

Yes it makes perfect sense now, thanks.
I was finding it hard to conceptualise, but thanks to your excellent explanation I understand now.

Regards,
JB