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 (Retired) Dictionaries Dictionary Iteration

(Stats Challenge) Code Check: Room for Improvement?

# 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(dict_teachers):
  trophyTeacher = ""
  mostClasses = 0
  numClasses = 0
  for teacher in dict_teachers:
    numClasses = len(dict_teachers[teacher])
    if numClasses > mostClasses:
      mostClasses = numClasses
      trophyTeacher = teacher
  return trophyTeacher

def num_teachers(dict_teachers):
  amountTeachers = 0
  for teacher in dict_teachers:
    amountTeachers += 1
  return amountTeachers

def stats(dict_teachers):
  new_list = []
  second_list = []
  numClasses = 0
  for teacher in dict_teachers:
    numClasses = len(dict_teachers[teacher])
    new_list = teacher.split('<string to list>')
    new_list.append(numClasses)
    second_list.append(new_list)
  return second_list

def courses(dict_teachers):
  new_list = []
  for courses in dict_teachers.values():
    for course in courses:
      new_list.append(course)
  return new_list

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Good job on the challenge. Your code works as is. I would give the following simplifications and some more Pythonic ways to code. See embedded comments:

# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}

# Recommended  by PEP-8 styling
# Use 4-space indentation instead of 2 spaces
# Use underscore instead of camelCase for variable names. For example:
#     num_classes instead of numClasses
#     most_classes instead of mostClasses

def most_classes(dict_teachers):
    trophyTeacher = ""
    mostClasses = 0
    # numClasses = 0 #<-- initialization not needed since it is set in each loop pass

    # alternative for loop structure using enurmerate
    # for teacher, class_list in enumerate(dict_teachers):
    for teacher in dict_teachers:
        # num_classes = len(class_list)
        numClasses = len(dict_teachers[teacher])
        if numClasses > mostClasses:
            mostClasses = numClasses
            trophyTeacher = teacher
    return trophyTeacher

def num_teachers(dict_teachers):
    # This entire function can be simplified to
    # return len(dict_teachers)
    amountTeachers = 0
    for teacher in dict_teachers:
        amountTeachers += 1
    return amountTeachers

def stats(dict_teachers):
    # new_list = [] #<-- initialization not needed
    second_list = []
    # numClasses = 0 #<-- initialization not needed

    # enumerate could also be used here
    for teacher in dict_teachers:
        numClasses = len(dict_teachers[teacher])
        # Since the split string is not present in 'teacher', no split will happen
        #   new_list = teacher.split('<string to list>')
        # instead simply start the new_list with 'teacher'
        new_list = [teacher]
        new_list.append(numClasses)
        second_list.append(new_list)
    return second_list

def courses(dict_teachers):
    new_list = []
    for courses in dict_teachers.values():
        # instead of looping through each course in courses, used the 'extend()' method
        #   for course in courses:
        #       new_list.append(course)
        new_list.extend(courses)
    return new_list

This is a first pass review. Feel free to post your updated code for more review. Keep up the good work!

Ah, I can see I made a bunch of silly mistakes! Thank you for your evaluation!