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

stats function - list of lists in Python

Please take a look at my script. I am getting more and more confused with this challenge...

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(teacher_dict):
    return len(teacher_dict)

def num_courses(teacher_dict):
    count = 0
    for arr in teacher_dict.values():
        for item in arr:
            count += 1
    return count

def courses(teacher_dict):
    courses = []
    for arr in teacher_dict.values():
        for item in arr:
            courses.append(item)
    return courses

def most_courses(teacher_dict):
    teachers_list = {}
    for teacher in teacher_dict:
        teachers_list[teacher] = len(teacher_dict[teacher])
    return max(teachers_list, key=teachers_list.get)

def stats(teacher_dict):
      for teacher, courses in teacher_dict.items():
          number_of_courses = []
          for key in teacher_dict:
          teacher = ''
          num = 0
          for item in teacher_dict[key]:
              num += 1
          number_of_courses.append((key,num))
      return big_list.append([teacher, number_of_courses])

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Maiia Spivak

I ran your code in my Python Shell, it seems you have some indentation errors.

Make sure you are using 4 spaces (not just 1 TAB) and if you use an advanced text editor or IDE, there is a way to make a TAB press equal to 4 character spaces instead of the actual whitespace character tab.

I fixed the indentation and your code is passing all but stats.

Thank you Chris. I already fixed the spacing, and I am still struggling with the stats function. Could you have a look at it? What is wrong?

Here is my another attempt:

def stats(teacher_dict): big_list = [] for teacher in teacher_dict.items(): number_of_courses = len(teacher_dict[teacher]) return big_list.append([teacher, number_of_courses])

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

So the problems I see with the stats code, without giving any answer. Ill try to guide you to the answer

def stats(teacher_dict):
    # imagine that teacher_dict holds the data below...
    # teacher_dict = {'Kenneth Love': ['Python Basics', 'Python Collections']}

    big_list = []
    for teacher in teacher_dict.items(): # .items() is a tuple of (key, value)
        # teacher is holding: ('Kenneth Love', ['Python Basics', 'Python Collections'])

        number_of_courses = len(teacher_dict[teacher]) # This wont work, because of what teacher above is holding

        return big_list.append([teacher, number_of_courses]) # return here immediately stops your loop and returns item

Thank you Chris! Will keep trying.