Alternative answer for consideration if you dont want to use functions [tag:1000331-6]

https://app.dataquest.io/m/1000331/python-data-analysis-basics-practice-problems/6/analyzing-game-sales-5

import csv
games=list(csv.reader(open("game_sales.csv")))[1:]

genres_by_publisher={}

publishers=[]
genres=[]
for g in games:
    if g[3] not in genres:
        genres.append(g[3])
    if g[4] not in publishers:
        publishers.append(g[4])
        
for p in publishers:
    genres_by_publisher[p]={}
    for genre in genres:        
        gamecount=0
        for g in games:
            if g[4]==p and g[3]==genre:
                gamecount+=1
                genres_by_publisher[p][genre]=gamecount
                
genres_ubisoft=genres_by_publisher["Ubisoft"]

most_popular_genre_ubisoft=""
for k,v in genres_ubisoft.items():
    if v==max(genres_ubisoft.values()):
        most_popular_genre_ubisoft=k

That is original. However IMHO functions and list comprehension are a better solution in terms of clean and reproducible code. Actually I would even try to replace the for loops of the sample solution with list comprehension. Just my 2 cents :slight_smile:

1 Like

didn’t like the original solution, think mine is pretty similar but I found it way easier to understand

opened_file = open('game_sales.csv')
from csv import reader
read_file = reader(opened_file)
data = list(read_file)
games = data[1:]
genres_by_publisher = {}

# step 1 create an empty freq table with all the publishers names:
publishers = {}
for p in games:
    publ = p[4]
    if publ not in publishers:
        publishers[publ] = []

#step 2 create a function, that generates a freq table for one publisher and their genres count:
def gen_pub(pub):
    genres_as = {}
    for i in games:    
        publisher = i[4]
        genre = i[3]
        if publisher == pub and genre not in genres_as:
            genres_as[genre] = 1
        elif publisher == pub and genre in genres_as:
            genres_as[genre] += 1
    return genres_as

#step 3 use the above function and loop over the freq list of publishers we created at step 1(to fill that freq table with )
for a in publishers:
    publishers[a] = gen_pub(a)

#step 4 pull out the Ubisoft data from the freq table
max_no = 0
most_popular_genre_ubisoft = ''
genres_ubisoft = publishers['Ubisoft']
for a in publishers['Ubisoft']:
    if publishers['Ubisoft'][a] > max_no:
        max_no = publishers['Ubisoft'][a]
        most_popular_genre_ubisoft = a

#ohh yeah forgot they were asking for this name of freq table:     
genres_by_publisher = publishers