Boolean Indexing With NumPy Second Challenge Question got error "list indices must be integers, not tuple"

Screen Link:

My Code:

trip_mph = taxi[:,7] / (taxi[:,8] / 3600)

speed_bool = trip_mph < 100

cleaned_taxi = []
cleaned_taxi.append(taxi[speed_bool])

mean_distance = cleaned_taxi[:,7].mean()

What I expected to happen:

The code should execute and give me the mean value of distance from cleaned_taxi list. The answer code worked well and is attached in the “other details” below. The only difference from my code than the answer code is that I created a new empty list and then appended it with cleaned data, but the answer code directly inputs the cleaned data into cleaned_taxi without creating one empty list.

What actually happened:

So in order to get a clean table with filtered data, I created an empty list called [cleaned_taxi] and appended it with the filltered [taxi] list. When I try to calculate distance’s mean value from cleaned_taxi list, an error message saying “list indices must be integers, not tuple” for my last line of code.

Answer code:

trip_mph = taxi[:,7] / (taxi[:,8] / 3600)
cleaned_taxi = taxi[trip_mph < 100]

mean_distance = cleaned_taxi[:,7].mean()
mean_length = cleaned_taxi[:,8].mean()
mean_total_amount = cleaned_taxi[:,13].mean()

Hi @gt1143

The append() method in python adds a single item to the existing list. Here you are trying to add a whole list to the empty list using append. Append would work if you were using some sort of iteration and adding one item at a time.

Since you are creating another list using the boolean mask, you can straight away assign it to cleaned_taxi. If you still want to use append(), think about getting each item in the taxi[speed_bool] and append it to the empty list.

Hope this helps.

1 Like

By this you are making 2d cleaned_taxi list while we are supposed to make 1D cleaned_taxi hence you got error TypeError: list indices must be integers, not tuple.

So you can do

mean_distance = cleaned_taxi[0][:,7].mean()

This is just to solve the indexing error but as expected list is 1D you will not able to submit mission. For that you will have to make list 1D by simply assigning direct as in answer.

1 Like