Genres_ft is shorter than expected

Screen Link:
https://app.dataquest.io/m/315/functions%3A-fundamentals/7/creating-frequency-tables

My Code:

# CODE FROM THE PREVIOUS SCREEN
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

def extract(index):
    column = []    
    for row in apps_data[1:]:
        value = row[index]
        column.append(value)    
    return column

genres = extract(11)

def freq_table(column):
    frequency_table = {}
    for value in column:
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
        return frequency_table
            
genres_ft = freq_table(genres)         

What I expected to happen:
genres_ftdict (<class ‘dict’>)
{‘Social Networking’: 167,
‘Photo & Video’: 349,
‘Games’: 3862,
‘Music’: 138,
‘Reference’: 64,
‘Health & Fitness’: 180,
‘Weather’: 72,
‘Utilities’: 248,
‘Travel’: 81,
‘Shopping’: 122,
‘News’: 75,
‘Navigation’: 46,
‘Lifestyle’: 144,
‘Entertainment’: 535,
‘Food & Drink’: 63,
‘Sports’: 114,
‘Book’: 112,
‘Finance’: 104,
‘Education’: 453,
‘Productivity’: 178,
‘Business’: 57,
‘Catalogs’: 10,
‘Medical’: 23}

What actually happened:

genres_ftdict (<class 'dict'>)
{'Social Networking': 1}

I tried to make the code as close as possible to the solution code and I still get only 1 key-value pair with only one instance of the key that has been recorded into the new dictionary

2 Likes

Hi @tdzivenu,

Welcome to the Community!

The issue in your code is with the return statement of the function freq_table. The return frequency_table should have the same indentation as the for-loop (for value in column:). In this case, you return your frequency table outside the loop, when it’s already completely formed.

3 Likes