Python Data Analysis Basics Practice Problems 3.Analyzing Game Sales 2, problem understanding the if not in loop

Hello dear fellow DQ members,

I hope you’re all well.
I would like your help for understanding some code please.
Within the practice mode for Data Analysis Basics, I am currently unable to understand the Dataquest solution for the third exercice, where we focus on the number of games for each platform.

I created a basic frequency table, and it is apparently correct
Here’s my solution :

def compute_games_platform(games):
    # creating a frequency table to count the number of games per platform
    games_per_platform = {}
    for game in games:
        platform = game[1]
        if platform in games_per_platform:
            games_per_platform[platform] += 1
            games_per_platform[platform] = 1
    return games_per_platform

but it is quite different from the provided solution which is

def count_games_per_platform(games):
    Function that creates a frequency table
    counting the number of games for each platform
    games_per_platform = {}
    for game in games:
        platform = game[1]
        if not platform in games_per_platform:
            games_per_platform[platform] = 0
        games_per_platform[platform] += 1
    return games_per_platform

and I feel completely stuck trying to understand the if not condition.
To me it feels unecessarily twisted in the sense that:

  • if not seems more complicated to read than an if condition (and I thought the readabilty of the code was a good practise to enforce)
  • and also, if the current platform is not in the frequency table, why would you set its value to zero to then one line later increment its value anyway
    Can’t wrap my mind around this syntax and it bugs me :persevere:

I don’t see how my solution and DQ one are equivalent.
What am I missing?

Thank you very much in advance for your help.
Have a nice day

1 Like

Hi Elsa,

Welcome to the Community! :rosette:

Please next time put a link to the mission screen here, so that people can help you faster.

Both your code and the one from the solution are absolutely equivalent in their functionality (creating a frequency table) and obtain the same results, even though in different ways. This if not concept here just reflects your else statement, only that in the solution code we first initialize this dictionary key (not existing before) with the value of 0, for then already in the next line to increment its value by 1 (you are right about it). On the other hand, this code line with the incrementation by 1 is always executed, whether the key was already in the dictionary or not. In case it was already presented in the dictionary, we just skip completely the if-statement and go directly to the line with the incrementation by 1.

Exactly for this task, in the great majority of cases platform will be already presented as a key in the dictionary, hence we’ll have just to increment its value by 1. To check every time if it’s there or not can be redundant: better to check, just the opposite, those rare cases when it isn’t in the dictionary yet. And it will also save you one line of code :slightly_smiling_face:

But of course, both yours and the solution code are good and correct in this case.

Hi Elena,
Thank you so much for your prompt reply and very clear explanation. It helped a lot, especially to think about the most frequent cases (and to not be reluctant to think in opposites) and as a result save a line of code

Greatly appreciated, and as often seems so simple with hindsight.

Many thanks

ps i did try to put a link following the very well put together template but somehow it only displayed the Dataquest homepage in the preview and not the exact practice mode screen I was referring to, so I deleted it to not confuse anyone but i’ll keep in mind for next time and look harder. Thanks

1 Like

You a very welcome, Elsa!
For your future questions, you might find these guidelines helpful, also about how to add the mission screen links.