# Order of dictionary changed. why?

``````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

Check out this and this

2 Likes

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

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