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 trialGavin Schilling
37,904 PointsCan't this be simplified using .sort()?
I could use something like
max_count = 0
for key, value in dictionary.items():
if len(value) > max_count:
max_count = len(value)
name = key
return name
But, can't this be simplified with .sort()?
# 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(dictionary):
return len(dictionary.keys())
def num_courses(dictionary):
return sum(len(values) for values in dictionary.values())
def courses(dictionary):
course_names = []
for values in dictionary.values():
course_names += values
return course_names
def most_courses(dictionary):
name = lists.sort(values)
return key[-1]
def stats(dictionary):
lists = []
for key, values in dictionary.items():
lists.append([key, len(values)])
return lists
1 Answer
Steven Parker
231,269 PointsI don't understand this syntax (neither did my Python program): "num_courses.pop(:)
".
But just conceptually, since you want to rank the entries by the number of values but return the key associated with them, it doesn't seem like there's any way to apply a sort that would lead to the required result.