LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Finding unique values in dictionary

Let’s consider again the count we did for the list ['4+', '4+', '4+', '9+', '9+', '12+', '17+']. To perform the count while finding the unique values automatically, we will:

  • Create an empty dictionary named content_ratings.
  • Loop through the list ['4+', '4+', '4+', '9+', '9+', '12+', '17+'], and check for every iteration whether the iteration variable (c_rating) exists as a key in content_ratings.
    • If it exists, then increment the dictionary value at that key by 1.
    • Else (if it doesn’t exist), create a new key-value pair in the content_ratings dictionary, where the dictionary key is the iteration variable (c_rating) and the dictionary value is 1.

Capture
https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/8/finding-the-unique-values

Hi all,

I am working through how to find unique values in dictionaries. In this case, content_ratings is an empty dictionary, which I was asked to create. However, in the for loop, check for every iteration whether the iteration variable (c_rating ) exists as a key in content_ratings . I am confused that if content_ratings is empty, how can you check whether there is any key exist? Please clarify and thank you.

Roni

An empty dictionary will return False when you check if any key exists.
But according to the loop logic, as you get False, a key will be added with the value of 1, so your content_ratings dictionary will start to get populated with key value pairs on the first iteration of the for loop.

1 Like

@whorolvani

Welcome to our community!

So I am going to try to explain using two examples.

You have the dictionary, adict. So when you loop through the dictionary with key in adict, the keys in the dictionary are called as show below.

image

To check if the dictionary contains a particular key, you can use the method below:
image

You can see that both empty and adict returns False when you search for Friday.

Imagine you loop through like this:

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

Remember that you can access the if c_rating in content_ratings: only when it returns True. Running c_rating in content_ratings checks if the that particular key is in the dictionary. This is very similar to print('Monday' in adict).

if c_rating in content_ratings return True, it means that key is in the dictionary. If it returns False, it means that key is not in the dictionary. For an empty dictionary, it returns False for the first iteration - and other iterations with keys not present in the dictionary.

When it returns False, it does not execute the if statement. Instead, it runs the else statement.

A more elegant way that I like to do this is using setdefault.

for row in apps_data[1:]:
    c_rating = row[10]
    content_ratings.setdefault(c_rating, 0)
    content_ratings[c_rating] += 1
1 Like

Thanks so much for your detailed explanation.

I print out each step in the for loop to help me better understand the mechanism.

In the first iteration, because ‘4+’ does not exist in content_rating (dictionary), content_rating[‘4+’] = 1

In the second iteration, ‘4+’ is already populated as a key paired with value of 1, so content_rating[‘4+’] ticked up to 2

correct?

1 2

1 Like

Yes, this is correct!

1 Like