Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Emil Hejlesen
Emil Hejlesen
3,014 Points

i'm stuck

someone please help

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.

dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
        'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_teachers(dict):
    num_teachers = 0
    for x in dict.keys():
        num_teachers += 1
    return num_teachers

def num_courses(dict):
    courses = 0
    for x in dict.values():
        for y in x:
            courses += 1
    return courses

def courses(dict):
    list = []
    for x in dict.values():
        for y in x:
            list.append(y)
    return list

def most_courses(dict):
    high = 0
    for teacher in dict:
        if len(dict[teacher]) > high:
            boss = []
            boss.append(teacher)
    return "".join(boss)


def stats(dict)
    total = []
    for x in dict:
        total.insert(list(x))

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

He Emil,

you are on the right track. You can use a for loop and the items() method. You just need to append a list to total where the key will be the first element of the list and the length of the value will be the second element.

Try it out and report back if you need assistance.

Emil Hejlesen
Emil Hejlesen
3,014 Points

hey i tried again and i still couldnt get it working:

this is my code: def stats(dict): total = [] for x in dict.items(): total.append(list(x))

return total

the only problem is that i dont return the length of the number of courses please help :)

Emil Hejlesen
Emil Hejlesen
3,014 Points

i just dont know how to get the length of the list added to the end

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi, you will get there Emil :smile: See my code:

def stats(dict):
    ls = [] # create a list to be modified
    for key, value in dict.items(): # loop over the dict and assign key and values
        ls.append([key, len(value)]) # append a list with key on index 0 and length of value on index 1
    return ls # returen a list of lists

Makes sense?

Yang Hu
Yang Hu
7,782 Points

hi, my method is kinda overly complicated because I haven't got to tuple yet... but it worked:

def stats(dict):
    master_list = []
    personal_dict = {}
    placeholder = 0

    for i in dict:
        personal_dict.update({placeholder: [i, len(dict[i])]})
        placeholder += 1

    for i in personal_dict.values():
        master_list.append(i)

    return master_list

But my goodness, Grigorij's code with the use of tuple is much easier and cleaner.