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

Alex Diaz
Alex Diaz
4,930 Points

Need help with this challenge!

This entire challenge has been really hard for me :p, but I need help getting the teacher with the most courses, I feel like i'm close but I still need help, thanks!

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.
def num_teachers(arg):
    return len(arg.values())

def num_courses(arg):
    total = 0
    for courses_list in arg.values():
        courses_for_teacher = len(courses_list)  # obtain number of courses for teacher
        total += courses_for_teacher  # add to total
    return total

def courses(arg):
    coursess = []
    for course in arg.values():
        coursess.extend(course)
    return coursess

def most_courses(arg):
    max_count = 0
    teacher = ''
    for count in arg.values():
        if len(arg.values()) > max_count:
            max_count = len(arg.values())
            teacher = arg.key()
    return teacher

4 Answers

Here's a couple useful hints that might point you in the right direction:

  • Hint 1:

To get the longest iterable (string, list, dictionary, etc.) in a list, you can do:

longest_string = ''

strings = ['a', 'ab', 'abc', 'abcd', 'ab', 'a']

for string in strings:
    if len(string) > longest_string:
        longest_string = string

How this works:

The program goes through every single element in the strings list, and if the current string is longer than the longest string recorded so far, then set the record long string to the current string.

it's just like fish. If you get a fish that's bigger than the world record size, you set the world record size to the size of that fish you found.

  • Hint 2:

You can iterate through every key/value pair in a dictionary by doing this:

(I'm just giving example data)

my_dict = {'name': 'Alex', 'age': 25, 'language': 'Python'}

for key, value in my_dict.items():
    print("{}: {}".format(key, value)

This will print:

name: Alex
age: 25
language: Python

Cool huh? I think these hints might push you in the correct direction :)

All you have to do is to put together these hints and you're good to go :smile:

I hope these help! ~Alex

Alex Diaz
Alex Diaz
4,930 Points

And once again, i'm stuck on the final step of this challenge...

def stats(arg):
    final_list = []
    for key, value in arg.items():
        final_list.append("{} {}".format(key, len(value)))
    return final_list

I'm getting a type error from trying to use that code. I think getting the keys and values into single list would be the best way to go and then append them all to "final_list" but I do not know how I would go about getting the keys and values into different lists.

This is what it wants me to do by the way- In this last challenge, I want you to create a function named stats and it'll take our teacher dictionary as its only argument. stats should return a list of lists where the first item in each inner list is the teacher's name and the second item in the number of courses that teacher has. For example, it might return: [["Kenneth Love", 5], ["Craig Dennis", 10]]

Why are you adding a string to the final_list variable when the challenge is asking to make a list of lists and not a list of strings? :smile:

If you need more hints, please ask below.

~Alex

Alex Diaz
Alex Diaz
4,930 Points
def stats(arg):
    final_list = []
    for item in arg.items():
        final_list.append(list(item))
    return final_list

Am I getting any closer Alexander? D:

:point_right: You need to use two variables for the for loop because you are iterating over a dictionary.

Also, you are adding the wrong item.

My solution:

def stats(teachers):
    result = []
    for teacher, courses in teachers.items():
        stats = [key, len(courses)]
        result.append(stats)
    return result

I hope this helps. ~Alex

Alex Diaz
Alex Diaz
4,930 Points

I got it by myself right before you posted your solution, lol. But other than that, thank you so much for helping me through that difficult challenge. Very appreciated!

No problem :)

def stats(teachers): result = [] for teacher, courses in teachers.items(): stats = [teacher, len(courses)] result.append(stats) return result

hello

Please see my solution. I hope it cn point you in right direction

dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics', 'Anothe Course'],'Kenneth Love': ['Python Basics', 'Python Collections']}
teacher = ""
number_of_courses = 0
for key,value in dict.items():
    #look at the key - values.   Just for demo.  you can delete the next 3 lines later
    print(key)
    print(value)
    print(len(value))
    #
    #now get counts and compare to number_of_courses, save latest
    if len(value) > number_of_courses:
        number_of_courses = len(value)
        instructor = key
print(instructor)

if this answers your question, please mark the question as answered

It doesn't help since you aren't explaining and you are just providing the answer without helping out.

Alex Diaz won't learn anything by copy-and-pasting code.

It's best to either:

  • Provide the answer and explain how it works step-by-step
  • OR just provide hints (this is usually better since they still have to find out the solution on their own but they get pointed the right direction)

~Alex

@Alexander Davison This is why you put print statements ... by printing the results of key items/lines in the code, student can learn by observing what each step is doing.

There is nothing more frustrating than getting no where.

In summary, you have your opinion and I have mine.

Read my post for an example of an answer giving hints :)

I used to be bad at this technique, but over the years, I improved slightly.

It's best to let the person try to make up the code themselves, since it makes them actually try harder.

People learn most by actually programming themselves

~Alex