How to sort a dictionary based on values (python mobile app project)

Hello everyone, I’m facing a problem sorting the result below. This is the last part of the mobile app project.

Below is my code for computing the average number of installs for each genre (category) and my attempt to sort the dictionary values in descending order.

categories_android = freq_table(android_final, 1)

for category in categories_android:
    total = 0
    len_category = 0
    for app in android_final:
        category_app = app[1]
        if category_app == category:            
            n_installs = app[5]
            n_installs = n_installs.replace(',', '')
            n_installs = n_installs.replace('+', '')
            total += float(n_installs)
            len_category += 1
    avg_n_installs = total / len_category
    print(category, ':', avg_n_installs)

I have tried to create a new list of avg_n_installs, and then use the function “sorted”, but the result is still not sorted and the numbers are in a separate line not in a single list.
Can someone help me with this? Thank you!

Here are the first 10 lines of my output:

ART_AND_DESIGN : 1986335.0877192982
AUTO_AND_VEHICLES : 647317.8170731707
BEAUTY : 513151.88679245283
BOOKS_AND_REFERENCE : 8767811.894736841
BUSINESS : 1712290.1474201474
COMICS : 817657.2727272727
COMMUNICATION : 38456119.167247385
DATING : 854028.8303030303
EDUCATION : 1833495.145631068
ENTERTAINMENT : 11640705.88235294

hi dzm1993,

I’m glad you found the Dataquest community.

As you know dictionaries do not store their keys in any particular order. This means you cannot sort a dictionary (as is). This is why you created a list of tuples with your code:

average_sort = sorted(average_dict, reverse = True)

Because this command is still in the for loop it is not able to sort the dictionary properly. To sort the dictionary values in descending order you will need to:

  1. Take the code you have for sorting the dictionary values out of your for loop.
    (remove this code average_sort = sorted(average_dict, reverse = True) & print(average_sort) )

  2. Import the operator module (more information on this module can be found here).
    import operator

  3. Create an empty dictionary outside of your for loop.
    unsorted_dic = {}

  4. Add your for loop.

  5. Outside of the for loop sort the dictionary values into a list of tuples and assign it to a new variable.
    sorted_dic = sorted(unsorted_dic.items(), key=operator.itemgetter(1), reverse=True)
    What the above code does is:
    a) returns list of a given dictionary’s (key, value) tuple pair. unsorted_dic.items()
    b) grabs the dictionary value and makes says these values is what should be used to sort on.
    key=operator.itemgetter(1)
    c) sorts in descending order
    reverse=True

This is what it should look like put all together (NOTE: change anroid_final to android_freeapp to match your code.).

import operator

categories_android = freq_table(android_final, 1)

unsorted_dic = {}

for category in categories_android:
    total = 0
    len_category = 0
    for app in android_final:
        category_app = app[1]
        if category_app == category:            
            n_installs = app[5]
            n_installs = n_installs.replace(',', '')
            n_installs = n_installs.replace('+', '')
            total += float(n_installs)
            len_category += 1
    avg_n_installs = total / len_category
    unsorted_dic[category] = avg_n_installs
    print(category, ':', avg_n_installs) # you may not want to print this

sorted_dic = sorted(unsorted_dic.items(), key=operator.itemgetter(1), reverse=True)

for item in sorted_dic:
    print(item)
1 Like

Hello Bvalgard,

Thank you so much for providing such a specific and detailed answer, your answer is clean and tidy, very easy to understand! very much respected!

Yes, the code exactly generates the output that I want. But there is still one small thing or detail that I don’t understand.

In my previous code for :

average_dict[avg_n_installs] = category      #Match each value to the key 
    average_sort = sorted(average_dict, reverse = True)   # sort the keys in the dictionary 
    print(average_sort)

the output is
[253542.22222222222]
[10801391.298666667]
[513151.88679245283]
[647317.8170731707]
[5074486.197183099]
[1437816.2687861272]
[1331540.5616438356]
[23253652.127118643]
[120550.61980830671]
[3695641.8198090694]

In the code that you have written

sorted_dic = sorted(unsorted_dic.items(), key=operator.itemgetter(1), reverse=True)

for item in sorted_dic:
    print(item)

the outputs format is (‘MEDICAL’, 120550.61980830671)

the output is tuples and not list. My only curious is why it’s generating tuples here and list in my coding ?:pleading_face:

My code is extracting the key and values of the dictionary and storing them as a tuple because of the .items() method in the following code:

sorted_dic = sorted(unsorted_dic.items(), key=operator.itemgetter(1), reverse=True)

Are you trying to get only the values sorted without the key?

2 Likes

I was looking for the values and key pairs. Just the output that your code brings out.
Thank you Bvalgard, you are awesome! Appreciate it. :hugs:

1 Like

I was stuck trying to figure this out for a long time yesterday. Thanks so much!!