I'm confused about this part 8.Finding the unique values (Dictionaries and frequent tables)

Screen Link:
image

the content_ratings is declared to be empty. however in the for loop there is an IF statement to check whether the key exist inside this empty dictionary ? Can someone explain ? Thank you

Yeah no key exists in content_ratings initially
Therefore In first execution of for loop, else statement will be executed
And after it
content_ratings[‘4+’] = 1

In second execution of the for loop
since ‘4+’ exists as a key, if condition will be true and hence
content_ratings[‘4+’]+=1

And so on for further iteration of for loop…

I hope this helps. :slightly_smiling_face:

2 Likes

@NguynTrungNgha: This program’s purpose to create a dictionary displaying the content rating and the corresponding number of apps with that particular content rating.

The variable content_rating is initialized as empty at first for future use. The loop indexes each element in the list ratings instead of indexing elements in the empty dictionary. It checks if the element already exists in the content_ratings dictionary. If it does not, it appends the key with a value of 1 in the dictionary. If it already exists, the count (value) of the key is simply incremented by 1.

image

Hope this clarifies!

2 Likes

thank you a lot !!!

Thank you a lot <3 <3

1 Like

@NguynTrungNgha: no worries! :slightly_smiling_face:

1 Like

Hi, there is also another question of mine :smiley: can you spot my error ??

@NguynTrungNgha: Will do my best… hahaha…

1 Like

Mention not :slightly_smiling_face:

1 Like

Hi,

If anyone could explain to me what else function is doing. I understood the counting with dictionaries but I’m unable to understand the else function here.

Thanks in advance

Hi @utkarshpande95:

The if statement checks if the c_rating is already in the contents_ratings dictionary. The else statement hence evaluates to True if the if condition is not met, that is if the c_rating is not already in the dictionary. The block is defined as content_ratings[c_rating] = 1 to simply initialize the key-value pair of the dictionary.

E.g. you have a list:

ratings = ['4+', '9+', '9+']

for the first run of the loop, content_ratings is empty (i.e. {})
Since 4+ is not already in content_ratings, the key-value pair is initialised
c_rating = {'4+' : 1}'

for the second run of the loop, 9+ is not already in content_ratings, the key-value pair is initialised
c_rating = {'4+' : 1, '9+' : 1}'

for the third run of the loop, 9+ is already in content_ratings, the so the value is incremented by 1
c_rating = {'4+' : 1, '9+' : 2}'

Hope this clarifies!

Hi @masterryan.prof:

Thanks for the explanation!
So I’ll try to reiterate just to confirm I’ve understood it correctly.
When the first time it runs there is no key or value hence if statement results in false and it moves to else statement hence it prints the first value as key and the corresponding count as 1 (as content_ratings[c_rating] = 1) then it moves in the loop and checks the next value as ‘4+’ or not if yes then increments, if not moves to else and adds its as a key and the corresponding count as 1.

is that right?

Thank you for your help.

Yup that is correct @utkarshpande95!

Why cant we switch the order of the if else statement, such as:

for row in apps_data[1:]:
      c_rating = row[10]
      if c_rating in content_ratings:
                    content_ratings[c_rating] =1
      else:
                     content_ratings[c_rating] += 1

I get the following error

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-b34d59f2298f> in <module>
     10                     content_ratings[c_rating] =1
     11       else:
---> 12                      content_ratings[c_rating] += 1
     13 
     14 print(content_ratings)

KeyError: '4+'

Is it because in the if statement, we are trying to check if c_rating in contents_ratings dict, then set that value = 1, else add 1 to it. However, for the first iteration, the 1st row is not in dict, so it goes to else statement & increases that value to 1. BUT, it can’t increment anything since you are NOT setting the value of the key equal to 1, but rather, incrementing a non-existent key value pair by 1? Please correct my logic here.

Essentially, i just want a concise explanation on why the if else statements cant be switched around like above. I think its because in the else statement, the content_ratings[c_rating] += 1 statement means that if the key is not in dictionary, then content_ratings[c_rating] = content_ratings[c_rating] +1, RHS is key + 1, but that would be typeerror because you can’t add a key with an integer, since you originally did not set content_ratings[c_rating] = 1?

@TravisBob: content_ratings at the first iteration of the loop is an empty dictionary, which you have initialised, meanwhile ratings is a list with values. So the loop starts going over the ratings list. It checks if the key exists in content_ratings. The intended behaviour is to increment the value (i.e. the “count”). iI the key already exists, it increments the corresponding value by 1. Otherwise, the key does not yet exist in the dictionary so it initialises the key-value pair.

Switching the blocks to be executed means that in the first iteration, since no key-value pairs exists, the if block will evaluate to False since there is no c_rating in content_ratings. So the else block will execute. Again here, you are assuming that the key already exists but it does not and thus the typeerror is thrown.

This is an incorrect assumption. We are trying to check if there is an existing key-element mapping between the dictionary and the list in each iteration of the loop.

In future, please open a new topic. I would also suggest you go over the differences between list and dictionaries and how dictionaries work again.