# Lists And For Loops : Alternative Way to Compute an Average

As i was doing this exercise , i realised that i received an error when i did a sum ,
this was initially my code :

``````    import numpy as np
opened_file = open('AppleStore.csv')

all_ratings = []
for i in apps_data[1:]:
rating = i
all_ratings.append(rating)
sum(all_ratings)
``````

After 10mins of trying to solve the problem , i decided to convert it to an array with numpy . I am unsure if this is the best practice. feel it is abit long winded . Any inputs ?

``````    import numpy as np
opened_file = open('AppleStore.csv')

all_ratings = []
for i in apps_data[1:]:
rating = i
all_ratings.append(rating)
all_rating = np.array(all_ratings, dtype=float)
avg_rating = sum(all_rating) / len(all_rating)
``````
1 Like

In your code, you are already setting the data type as a float:

`np.array(all_ratings, dtype=float)`

Which is exactly what was needed even without using `numpy` as the solution. You need to convert the value to a float before appending it to the list like this:

``````opened_file = open('AppleStore.csv')

all_ratings = []
for i in apps_data[1:]:
rating = float(i)
all_ratings.append(rating)
avg_rating = sum(all_ratings) / len(all_ratings)
``````

By the way, that doesn’t mean your approach to the solution is bad. In fact, I found it very interesting that you were able to come up with a different approach to the problem. Great job!

Best,
Sahil

1 Like