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 trialMaiia Spivak
3,038 PointsPlease help. Spent too much time on this step, and feel stuck..
Stats function attempt is at the very bottom of my code. Could you please help me figure this out?
# 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.
import collections
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):
big_list = []
for teacher in teacher_dict.items():
number_of_courses = len(teacher_dict[teacher])
return big_list.append([teacher, number_of_courses])
2 Answers
Josh Hunt
3,572 PointsHey Maiia,
You're really close. It's a tough one but I finally figured it out.
Basically, it's a syntax thing. Every time you go through the loop you're creating an empty list. In the loop, the "key" of the teacher (their name) and the length of their "value" (number of courses) gets appended to the new empty list. The contents of that smaller list get appended to the bigger list. Then we return that bigger list. I hope that makes sense.
def stats(teachers):
teacher_and_courses = [] # the big list that we'll append our teachers and course count to
for item in teachers:
current_teacher = [] # this list starts empty every time the loop runs
current_teacher = [item, len(teachers[item])] # we add the key and value of the item to the list
teacher_and_courses.append(current_teacher) # then it gets added to the bigger list
return teacher_and_courses # boom. done.
john larson
16,594 PointsHey Josh, nice answer :D. I just struggled through all these (again). Here is one that is a little more concise.
def stats(teacher_dict):
big_list = []
# teacher/courses on one line
for teacher, courses in teacher_dict.items():
# append teacher/len(courses) on one line
big_list.append([teacher, len(courses)])
return big_list
Josh Hunt
3,572 PointsCool, thanks John!
Maiia Spivak
3,038 PointsThank you John! Looks much better!
Thomas Helms
16,816 Points+1 to John as well. I guess I must have fallen asleep during the .items() video. I had:
for teacher in teacher_dict.items():
and was getting unhashable type errors. I forgot both key and value.
john larson
16,594 Pointsjohn larson
16,594 Points:D