Not understanding how we are checking to see if a key exists in an empty dictionary

Hello! I’m having some difficulty understanding the following:

  1. I don’t understand how we are checking to see if a key exists in an empty dictionary from a list.
  2. Why is the c_rating variable in brackets?
content_ratings = {}
ratings = ['4+', '4+', '4+', '9+', '9+', '12+', '17+']

for c_rating in ratings:
    if c_rating in content_ratings:
        content_ratings[c_rating] += 1
    else:
        content_ratings[c_rating] = 1
    print(content_ratings)

print('Final dictionary:')
print(content_ratings)

Hi all!
I received an answer that really helped. I’ll post it for anyone that has the same issue:

‘’’
Let’s go through each iteration of the loop to understand how the content_ratings dictionary is built.

First iteration:

  • c_rating is equal to '4+'.
  • Since content_ratings is an empty dictionary, '4+' is not in the content_ratings dictionary, so the else block is executed.
  • A new key-value pair is added to the content_ratings dictionary, where the key is '4+' and the value is 1.

Second iteration:

  • c_rating is equal to '4+' again.
  • Since the key '4+' is already in the content_ratings dictionary, the if block is executed.
  • The value associated with the key '4+' is incremented by 1, so content_ratings becomes {'4+': 2}.

Third iteration:

  • c_rating is equal to '4+' again.
  • Since the key '4+' is already in the content_ratings dictionary, the if block is executed.
  • The value associated with the key '4+' is incremented by 1, so content_ratings becomes {'4+': 3}.

This process continues for each iteration until all elements in the ratings list have been processed. The final result of the code is a dictionary content_ratings where the keys are the unique ratings from the ratings list and the values are the counts of each rating.
‘’’

-Jason

In these situations EAFP style is suitable:

Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

In Python code it could look like:

>>> content_ratings = {}
>>> ratings = ['4+', '4+', '4+', '9+', '9+', '12+', '17+']
>>> for rating in ratings:
...     try:
...         content_ratings[rating] += 1
...     except KeyError:
...         content_ratings[rating] = 1
...
>>> content_ratings
{'4+': 3, '9+': 2, '12+': 1, '17+': 1}

For counting there is built-in Counter in built-in collections module which has some useful methods as well:

>>> from collections import Counter
>>> ratings = ['4+', '4+', '4+', '9+', '9+', '12+', '17+']
>>> content_ratings = Counter(ratings)
>>> content_ratings
Counter({'4+': 3, '9+': 2, '12+': 1, '17+': 1})
>>> content_ratings['4+']
3
>>> content_ratings.most_common()
[('4+', 3), ('9+', 2), ('12+', 1), ('17+', 1)]
>>> content_ratings.most_common(1)
[('4+', 3)]