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 trialRosu Calin
935 PointsI feel stuck. Can someone offer a hand?
I've really tried making this work on my laptop but I can't seem to find a solution.
I know I need to: 1) See how long each key value is 2)Somehow compare them 3)Return the key of the longer key value
# 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(dic):
return len(dic.keys())
def num_courses(dic):
sum = 0
for key_value in dic:
sum += len(dic[key_value])
return sum
def courses(dic):
new_list = []
for key_value in dic:
new_list.extend(dic[key_value])
return new_list
def most_courses(dic):
for key in dic.keys():
x = len(dic[keys])
2 Answers
Steven Parker
231,271 PointsLet's look at your objectives individually:
1) See how long each key value is
Simple enough, you did this already in num_courses, using the len function.
2) Somehow compare them
You could use an if statement with a "greater than" (>
) comparison operator between the two values.
3) Return the key of the longer key value
In your loop, you could keep both the longest length and the name (key) that goes with it. Then, when the loop finishes you just return that retained name.
Ronit Mankad
12,166 PointsSimple logic:
def most_courses(dic):
max_len = 0
teacher_with_most_courses = ''
for key in dic.keys():
if len(dic[key]) >= max_len:
max_len = len(dic[key])
teacher_with_most_courses = key
return teacher_with_most_courses