Brackets for adding value to dictionaries

I’m currently learning about how to add values to a dictionary. This is the code givien:

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
content_ratings ={“4+”:0, “9+”:0, “12+”:0, “17+”:0}
for row in apps_data[1:]:
c_rating = row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1

Why are there brackets around c_rating (i.e., [c_rating] )? I thought brackets were used with elements or index numbers only.

hi @mtl1212

Please read this article. In case your doubts are still not clear, please do respond.

1 Like

Hi Rucha,
So, what’s in brackets, when it’s a dictionary is the name of the key we are using? The format is always “Name of dictionary”[key name] += 1?

Is that right or am I still off?

1 Like

Hi @mtl1212

Yup that’s correct. In the bracket goes, the key name you want to access, manipulate or do something.

Hi @mtl1212

Maybe you already know the points I am going to mention, but I am going to do it anyway for the sake of easier explanation.

  • A dictionary works in key:value pairs. ie inside a dictionary, a value is stored in connection with its key. So in order to access the value, we need to call the key.
Dict={"key1":"apple", "key2":"orange","key3":"mango"}

To get to mango, you have to access key3 of dict


Will give an output “mango”

  • Now if we want to add a new value, we need to add a key along with it. ie if we need to add grapes we need to define a new key as well. We do it this way.

When you call Dict it will give
{'key1': 'apple', 'key2': 'orange', 'key3': 'mango', 'key4': 'grapes'}
the updated dictionary.

  • Now, we know these keys we used should be unique. If there are two same keys within a dictionary, you know the confusion that it will create. So for example ‘Key1’ can not come twice within the dictionary.

  • But if you try to do that, what will happen next is, the value connected with the key gets updated. This happens because dictionaries are Mutable which means, you can change the values. At the same time ** dictionary keys are immutable** which means you cannot change the values of keys.

  • We can use this detail to our advantage when it comes to counting something or to create a frequency table.

Let us take the example given by @Sahil in the post shared by @Rucha

letters = ['A', 'B', 'B', 'B', 'A', 'C', 'D', 'D']

We have a list called letters and we need to count how many time each unique lettes appear. Without our superior human mind we instantly know that the unique letters are A, B , C and D, and it appears 2,3 1, 4 respectively.

Since we have figured out the unique letters (which needs to be counted) those are going to be the immutable keys that we are going to use in our dictionary.

The value connected with these keys will be their frequency (ie number of times it appears inside the list). Since dictionary values are mutable, which means it can be updated, we can use a loop to run through the whole list to find each letters and its frequency.

So we are going to store the value in this empty dictionary letter_count = {}

for letter in letters:
    if letter in letter_count:
        letter_count[letter] += 1
        # equivalent to letter_count[letter] = letter_count[letter] + 1
        letter_count[letter] = 1

Now we run the loop and see if the first letter in the list is inside the dictionary.
if letter in letter_count:
The first letter in the list is “A” and since the letter count dictionary is empty it cant find it there. So it skips the line and goes to else conditon.

        letter_count[letter] = 1

This becomes
So the empty dictionary now becomes `{A:1}

So in the subsequent iterations when the condition detects another “A” this line gets executed

if letter in letter_count:
        letter_count[letter] += 1

ie letter_count[A]+=1

then the dictionary value gets updated and stored as

  • So to answer your question the format is
# for defining a new key:value pair

#for updating a new key:value pair
dictionary_name[unique_key_name]+=new value

I hope this helps

1 Like

@Rucha thanks so much!

1 Like

Thanks so much for you (very!) detailed answer. I think I’m getting the hangout of it now!