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
else:
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
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
Elsa