Question regarding my the first guided project: Profitable App Profiles for the App Store and Google Play Markets

Hi! I’m just a beginner in all of these so please don’t judge me if ever my question is too silly or too obvious :smiley: Any help and feedback will be appreciated.

So I was going through my first guided project regarding “Profitable App Profiles for the App Store and Google Play Markets” and I observed that I have different result in a particular code cell. Specifically, this is the part that calculates the average number of user ratings per app genre on the App Store that the following code:


genres_ios = freq_table(ios_final, -5)

for genre in genres_ios:
    total = 0
    len_genre = 0
    for app in ios_final:
        genre_app = app[-5]
        if genre_app == genre:            
            n_ratings = float(app[5])
            total += n_ratings
            len_genre += 1
    avg_n_ratings = total / len_genre
    print(genre, ':', avg_n_ratings)

The values of my result (i.e. the average rating per unique genre) are similar to the solution’s result, but the ordering is different. Why is the order different? I attached a screenshot of my result and a solution’s result below.

Solution’s Result:

My Result:

Thank you for your help!

Hello @imari.borda2018

Thanks for posting the issue.

As per my understanding of your code, the reason could be once you calculate the average number of average ratings then you need to store the values in unsorted dictionary. The next step is to sort the dictionary values into a list of tuples and assign it to new variable then you will the sort values in order as presented in the solution. Please see the below code for your understanding.

genres_ios = freq_table(Ios_final, -5)

unsorted_genres_ios = {}

for genre in genres_ios:
total = 0 # Store sum of ratings for each genre
len_genre = 0 # Store number of apps specific to the genre
for app in Ios_final:
genre_app = app[-5]
if genre_app == genre:
n_ratings = float(app[5])
total += n_ratings
len_genre += 1
avg_n_ratings = total / len_genre # Compute the average number of user rating
unsorted_genres_ios[genre] = avg_n_ratings # Store the value to an unsorted dictionary

Sort the dictionary values into a list of tuples and assign it to a new variable

sorted_genres_ios = sorted(unsorted_genres_ios.items(), key=operator.itemgetter(1), reverse=True)

for item in sorted_genres_ios:
print(item)

Also, you can take a look at the Technical Question Guidelines to see how to write a better topic in order to receive faster, more accurate answers.

I hope this helps.

Kindly let me know if you have any further question.

Best
K!

1 Like

Hi @imari.borda2018
Regarding your question, you can also refer to this reply.


In addition to that dictionary keys are immutable and keys are always unique, so logically you do not need any fixed index (or ordered index like list) to call value, you can just call value by key irrespective of its position. so no ordering is needed in the dictionary (it’s the default behaviour of dictionary).

Hope it helps!
Let me know if you have further questions.

1 Like

Oh yeah! I forgot this unique feature of dictionaries. Your comment is really helpful, thank you very much! I got stuck thinking about it for hours!

1 Like

Thank your very much for your reply! Your code gave me some idea how to otherwise arrange my results! Thanks also for pointing out the “Technical Questions Guidelines”, it’s a very helpful read :smiley:

1 Like

Awesome! :blush:

Glad my post was helpful. Please see if this post helps regarding marking a post as solution: How do I mark a post as solved?

Best
K!