Functions Fundamentals: Screen 9 - Unexpected code result

Screen Link:
https://app.dataquest.io/m/315/functions%3A-fundamentals/9/reusability-and-multiple-parameters

My Code:

def freq_table(data_set, index):
    frequency_table = {}
    
    for row in data_set[1:]:
        elements = row[index]
        if elements in frequency_table:
            frequency_table[elements] += 1
        else:
            frequency_table[elements] = 1
        
        return frequency_table
    
ratings_ft = freq_table(data_set=apps_data, index=7)

What I expected to happen:

I expected the ratings_ft dictionary to be filled with sums of each rating value.

What actually happened:

ratings_ftdict (<class 'dict'>)
{'3.5': 1}

I just seem to only get one key value pair for some reason. Can someone explain why my code is doing this? I’ve compared it to the answer and it seems identical save for a difference in parameter names, which I figured should not affect the code.

Thanks,

-Arvin

1 Like

@arvinabedi welcome back!

Kindly move your return to align with for. With the way the return is presently, the for loop will stop after the first iteration and return whatever result.

def freq_table(data_set, index):
    frequency_table = {}
    
    for row in data_set[1:]:
        elements = row[index]
        if elements in frequency_table:
            frequency_table[elements] += 1
        else:
            frequency_table[elements] = 1
        
    return frequency_table
    
ratings_ft = freq_table(data_set=apps_data, index=7)
1 Like

Haha! Thanks so much, I knew it had to be something little. I appreciate the quick response and glad to be back!

1 Like