# Python Dictionaries and Frequency Tables 4/11

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
print(content_ratings)

Why does the above code return empty lists when used without else?

Firstly, let’s look at what the `if condition` does.
`if c_rating in content_ratings: content _ratings[c_rating] += 1`

We’re checking if the c_rating is in content_ratings. The content_ratings is empty, meaning c_rating doesn’t exist at all. So, the if condition never gets executed. And for all the iterations, it’s the same. Thus, it results in empty lists.

I don’t get it… why doesn’t the condition execute when the code is alright?? And secondly, why doesn’t it show an empty list when used with else:

INPUT:
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)

OUTPUT:
{‘4+’: 1}
{‘4+’: 2}
{‘4+’: 3}
{‘4+’: 3, ‘9+’: 1}
{‘4+’: 3, ‘9+’: 2}
{‘4+’: 3, ‘9+’: 2, ‘12+’: 1}
{‘4+’: 3, ‘9+’: 2, ‘12+’: 1, ‘17+’: 1}

My understanding is, that when used with else it does not show redundant values (which is right in this case) but when I don’t use else, why does it show just empty lists and not the same output as above + redundant values??

The `content_ratings` is not a list, it’s a dictionary. It consists of key-value pairs of content rating and number of times it has occured.

Firstly, understand that every conditional statement gets executed based on either of the Boolean values - True or False

``````if conditional_statement: # evaluates to True or False
do_something: # performs an action
``````

Now, coming to your question, the code in your main question:

``````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
print(content_ratings)
``````

You are writing a for loop which loops through the ratings list containing all the ratings. Then in the next block of code, you write a conditional statement stating to check if `c_rating` exists in `content_ratings`. Does it exist as a key in the dictionary? Nope. So, the code `content_ratings[c_rating] += 1` doesn’t get executed because the above conditional test failed. Now, you only have a single if statement, so Python doesn’t have other alternate action to perform. So, it goes to the next element and the same mentioned above repeats.

Now, let’s go to the next code with the else statement.

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

Now, in this case, when the loop runs for the first time, first it checks if the `c_rating` exists in `content_ratings` as a ‘key’. Does it exist? Nope. So, the if conditional statement fails, but Python doesn’t go to the next element of the `ratings` list because you have mentioned in the else block that if the first conditional statement fails, it should set a value of 1 in the dictionary, so, it becomes:

``````content_ratings = {'4+': 1}
``````

And subsequently, the same happens if the key doesn’t exists, and if it exists, it will get incremented by 1. I hope, I have given the explanation correctly. If not, @Elena_Kosourova or someone else can check if there’s an easier way to explain this. 1 Like

Thanks @vishallbabu5 for the detailed answer. I am trying to make some sense of it… I do understand the logic of else statement that if the first conditional statement fails, it should set a value of 1 in the dictionary… but now I am confused why it sets +=1 above with if syntax… I mean what’s the like-wise context in this scenario??

The if statement doesn’t set an initial value of 1. The else block does that job. The if statement increments the value by 1. It’s similar to writing:

``````content_ratings[c_rating] = content_ratings[c_rating] + 1
``````

So, if there’s already a value set to 1, when the second time the same rating occurs, it doesn’t make sense to set the value 1 again, right? So we add 1 to it. Meaning it has appeared 2 times.

Here’s an article written in the community explaining about dictionary.

Hi @mursuljamil ,

Great question! The “if statement” will never be triggered because c_rating is not in content_ratings dictionary. The dictionary is empty and will never have any values until a value is added. So the “if statement” will never be triggered and the dictionary will remain empty until you add the else clause or another elif clause.

As you loop through the ratings list the values are stored in c_rating . So in the first iteration of the for loop, c_rating = “4+” the first item in the ratings list.

The content_rating {} dictionary that you defined on your first line is empty “content_ratings = {}”.
The “if statement” will only be triggered if c_rating is in the content_ratings {} dictionary.
When you run the first for loop and c_rating = ‘4+’ , the "if statement is not triggered because c_rating which is "4+’ is not in content_ ratings {} dictionary. Content_ratings {} dictionary is still empty. No value has been added to the dictionary.

You need the else clause so you can add the value from c_rating to content_ratings{} dictionary. Then on the next for loop, the if statement will be triggered and it will add +1 to the 1 value that is already in content_ratings{} dictionary.