Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries Teacher Stats

Michael Spinks
Michael Spinks
1,150 Points

Please can you provide some clarity of the dictionary of teachers?

I have tried passing the dictionary of teachers and iterating through the items to append to a list. However this breaks task 1. I am returning the list of courses. Not sure where I am going wrong.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dicts):
  my_dict = {}

  for i, j in dicts.items():
        #print i
        my_dict[i] = i
      # print my_dict[i]
        count = 0
      # add i as the value in our own dictionary
        for k in j:
      #     print k
            count += 1
      # print count 
        my_dict[i] = count

  return max(my_dict, key=my_dict.get)

def num_teachers(teachers):


    max_count = 0
    max_count = len (teachers)

    return max_count    

def stats(dicts):   

    list_of_lists = []
    stat_list = []

    for i, j in dicts.items():
        count = 0
        for k in j:
            count += 1
        stat_list = [i,count]
        list_of_lists.append (stat_list)

    return list_of_lists    

def courses(teachers):

  course_list = []

    for i, j in teachers.items():
        for k in j:
            print k
            course_list.append(k)

    return course_list

2 Answers

Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

Your algorithm looks fine. You are missing a parenthesis in the print statement in your last function, if you intended to print it. Else, comment that line out. Your code worked in my console.

For your reference, this is the code that I ran.

# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dicts):
    my_dict = {}

    for i, j in dicts.items():
        #print i
        my_dict[i] = i
        # print my_dict[i]
        count = 0
        # add i as the value in our own dictionary
        for k in j:
            #     print k
            count += 1
        # print count
        my_dict[i] = count

    return max(my_dict, key=my_dict.get)

def num_teachers(teachers):

    max_count = len (teachers)

    return max_count

def stats(dicts):

    list_of_lists = []
    stat_list = []

    for i, j in dicts.items():
        count = 0
        for k in j:
            count += 1
        stat_list = [i,count]
        list_of_lists.append (stat_list)

    return list_of_lists

def courses(teachers):

    course_list = []

    for i, j in teachers.items():
        for k in j:
            print(k)
            course_list.append(k)

    return course_list


my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
    'Kenneth Love': ['Python Basics', 'Python Collections']}
print(most_classes(my_dict))
print(num_teachers(my_dict))
print(stats(my_dict))
print(courses(my_dict))
Michael Spinks
Michael Spinks
1,150 Points

Thanks for your help. All done now.