How to calculate the new avg at once?

under_100_m =

The answer sheet shows how to remove outliner to calculate new avg for each category. But it takes too much time to calculate each category, how to run a code to do all category new avg calculation at once?

and after we do the avg, how to sort the outcome ?

for app in android_final:
n_installs = app[5]
n_installs = n_installs.replace(’,’, ‘’)
n_installs = n_installs.replace(’+’, ‘’)
if (app[1] == ‘COMMUNICATION’) and (float(n_installs) < 100000000):
under_100_m.append(float(n_installs))

sum(under_100_m) / len(under_100_m)

Hello @candiceliu93

As per my understanding your code should follow like this:

categories_android = freq_table(android_final, 1)

unsorted_categories_android = {}

for category in categories_android:
total = 0 # Store sum of installs for each genre
len_category = 0 # Store number of apps specific to the genre
for app in android_final:
category_app = app[1]
if category_app == category:
n_installs = app[5]
n_installs = n_installs.replace(’,’, ‘’) # Remove commas
n_installs = n_installs.replace(’+’,’’) # Remove plus signs
n_installs = float(n_installs)
if n_installs < 100000000: # Exclude apps with over 100 million installs
total += float(n_installs)
len_category += 1
avg_n_installs = total / len_category # Compute the average number of installs
unsorted_categories_android[category] = avg_n_installs # Store the value to an unsorted dictionary

#Sort the dictionary values into a list of tuples and assign it to a new variable
sorted_categories_android = sorted(unsorted_categories_android.items(), key=operator.itemgetter(1), reverse=True)

for item in sorted_categories_android:
print(item)

Upon executing the above code, you should be able to see ,after excluding apps with over 100 million installs, we see that Photography category now rises to be the most popular genere

Just in case, if your issue is still on, i request you to share the mission link followed by your code so that we can figure it out better and solve accordingly.

Best
K!

thank you for helping!! @prasadkalyan05

I dont understhand this part, how to use the “sort” build-in function. Can you help to interpret the code?

> #Sort the dictionary values into a list of tuples and assign it to a new variable
> sorted_categories_android = sorted(unsorted_categories_android.items(), key=operator.itemgetter(1), reverse=True)

Hey !

Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable. A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:

#Sort the dictionary values into a list of tuples and assign it to a new variable
sorted_categories_android = sorted(unsorted_categories_android.items(), key=operator.itemgetter(1), reverse=True)
In our case, after calculating the average numbers of installs then we are storing those values into our unsorted_categories_android dictionary(which is unsorted). Next we are applying the sorted function to sort the unsorted dictionary values into a list of tuples. And also when we pass the parameter , reverse = true in sorted function , we will see the values in descending order(meaning that highest to lowest).

After sorting the dictionary, when we print the items in dictionary you should be able to see them in descending order.

I hope this helps you.

Let me know your status accordingly

Best
K!

Hi @prasadkalyan05! thank you for explaining it. Could you give me more detailed information on how to use it, one more example will be helpful. I insert the questions into the code.

sorted_categories_android = sorted(unsorted_categories_android.items(),((in this place i have to put the file name and empty bracket means?)) key=operator.itemgetter(1), ((key has to be operator **
** and 1 means? ))reverse=True(( this one i unsderstand, thank you!!))
)

Thank you in advance!!