Still can't wrap my head around looping with a dictionary

Screen Link: Learn data science with Python and R projects

When creating a frequency table for the genres of apps in a table I can’t wrap my head around how we can extract the genres from genre counting since it is an empty dictionary?

Why do we use if genre in genre_counting if genre_counting is empty? I know this has been answered previously but the solution didn’t click with me. Is it because if we use the empty dictionary we are automatically assigning 1 to the genres and then adding another 1?

genre_counting={}

for row in apps_data[1:]:
genre=row[11]
if genre in genre_counting:
genre_counting[genre] +=1
else:
genre_counting[genre]=1
print(genre_counting)

Thank you!

1 Like

That’s mostly correct.

if genre in genre_counting:

The above if statement checks if genre_counting has the key genre in it or not. If it does, we update the value corresponding to that genre key by 1. If it does not, we define the key and set the value to 1.

It’s not particularly about the dictionary being empty, but, instead about whether that key is present in the dictionary or not.

2 Likes

Thank you. I’m still having trouble understanding why we perform:

if genre in genre_counting

and not: if genre in apps_data.

There aren’t any genres in genre counting because it is an empty dictionary right? Why aren’t we iterating through apps_data?

That’s correct. We want to fill up the genre_counting dictionary with genres and their corresponding frequency of occurrence.

We are iterating through apps_data using the for loop.

If we were to use

as you asked, how would that work as per you?

  • apps_data is a list of lists.
  • genre is row[11] where row is a list in apps_data. So, genre is the genre in every row of our list of lists.

if genre in apps_data. would be trying to see if a genre in a specific row is inside a list of lists, which would not work. You can try that yourself with some toy code:

samples = [[1, 2, 3], [4, 5, 6]]

for row in samples:
    item = row[0]

    if item in samples:
        print("do something")

What do you think the above code will output? It’s similar to what you are currently suggesting. What would item in samples mean? If item is 1 at any point, what would if 1 in samples yield - True or False? Spend some time thinking about this. If needed, feel free to go back and check out the content around lists, using if and what in does in Python.

Since we want to populate the dictionary,

  • we simply iterate over the list of lists,
    • for every list (or row) in that list of lists we check if the genre is in the dictionary or not.
      • If it is not, we add it to the dictionary.
      • If it is already there, we update the corresponding value.

I would recommend that you think about your alternative implementation in more detail. Don’t think of individual steps, think about all of the code and how it would execute. It might be possible you have a different approach to do this, but it might be a complicated one. Feel free to ask more questions!

3 Likes

thank you for the help!