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 trialDhruv Ghulati
1,582 PointsCan't append key, len(value) to a list (append only takes one argument) - stuck with stats function
Hi , anyone know what's wrong with my code here? I tried append to add to a list one by one but that didn't work, now using list('string') to basically define what each item will be, but this isn't working. Not sure - do I need to define a new empty list of lists first?
# 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(my_dict):
max_count = 0
teacher = ""
for key, value in my_dict.items():
if len(value) > max_count:
teacher = key
max_count = len(value)
return teacher
def num_teachers(teacher_dict):
return len(teacher_dict)
def stats(t_dict):
statslist=list()
for key,value in t_dict.items():
statslist(key)=len(value)
return statslist
1 Answer
Kenneth Love
Treehouse Guest TeacherLists don't have keys and, even if they did, you wouldn't use parentheses to access them, you'd use square brackets.
Dhruv Ghulati
1,582 PointsDhruv Ghulati
1,582 PointsThanks a lot Kenneth! Figured it out! Loving this.
I was having a problem as it was appending everything into the main list one by one (instead of forming a list of lists), then I figured out this sublist method for each iteration.