Order of dictionary changed. why?

Screen Link:

I have not attempted this exercise yet. I would like some help to understand the example:

content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
c_ratings_proportions= {}

for loop interating through content_ratings:
      populate c_ratings_proportions dict

why is the output of  c_ratings_proportions and also content_ratings in a different order to that of the original iterable content_ratings
1 Like

@paddyd101

Check out this and this

2 Likes

Hi @paddyd101
please can you elaborate on the kind of order you are talking about ,checking your code which isn’t that clear, I guess you are referring to the output.

Am not that good but I will try my best to help understand .So there are only two ways key-value pairs in dictionary can be displayed that is either horizontally or vertically.

If you check at your dictionary content_rating, key-value pairs are arranged horizontally, that is

{'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}

which is exactly the same order as the output of the c_ratings_proportions and the c_ratings_percentages.

here is the code

content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197
c_ratings_proportions={}
c_ratings_percentages={}
for key in content_ratings:
    proportion = content_ratings[key] / total_number_of_apps
    percentage = proportion * 100
    
    c_ratings_proportions[key] = proportion
    c_ratings_percentages[key] = percentage

if you use the print ()command to display the two dictionary that is c_ratings_proportions and c_ratings_percentage the output will be displayed horizontally.
let’s just display one of the dictionary;

print(c_ratings_proportion)

output

{'4+': 0.6159510907322495, '12+': 0.16048353480616923, '9+': 0.13714047519799916, '17+': 0.08642489926358204} 

And from the output, we can confirm that both content_ratings and c_ratings_proportion have the same order, the key-value pairs are arranged horizontally.

Let me know if this was helpful.

2 Likes

Hi @brayanopiyo18,

thanks for your reply.

Yes, I am referring tot he output.
Am I missing something in the example code on this mission?
Is there an error in the example provided?
The order of the key:Value pair is displayed differently in the output compared to the initial iterable dictionary - content_ratings

The initial content_ratings = {β€˜4+’: 4433, β€˜9+’: 987,β€˜12+’: 1155, β€˜17+’: 622}

Note the order of the keys - β€˜4+’, β€˜9+’, β€˜12+’, β€˜17+’

and the output (in the example) is:
{β€˜9+’: 0.13714047519799916, β€˜12+’: 0.16048353480616923, β€˜4+’: 0.6159510907322495, β€˜17+’: 0.08642489926358204}
{β€˜9+’: 987, β€˜12+’: 1155, β€˜17+’: 622,β€˜4+’: 4433}

note the order of the keys is now:
c_ratings_proportions β€˜9+’, β€˜12+’, β€˜4+’, β€˜17+’
content_ratings β€˜9+’, β€˜12+’,β€˜17+’, β€˜4+’

Since my original post, I ran the code provided and I got a different output to that shown in the example. The output which I have now does make sense to me, in that it is in the same order. Much like the example you provided.

Here is my code below:

content_ratings = {β€˜4+’: 4433, β€˜9+’: 987, β€˜12+’: 1155, β€˜17+’: 622}
total_number_of_apps =7197
c_ratings_proportions={}

for key in content_ratings:
proportion = content_ratings[key]/total_number_of_apps
c_ratings_proportions[key]=proportion

print(c_ratings_proportions)
print(content_ratings)

and output:
{β€˜4+’: 0.6159510907322495, β€˜9+’: 0.13714047519799916, β€˜12+’: 0.16048353480616923, β€˜17+’: 0.08642489926358204}
{β€˜4+’: 4433, β€˜9+’: 987, β€˜12+’: 1155, β€˜17+’: 622}

I hope this question makes sense.

1 Like