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 trialeestsaid
1,311 PointsCode Challenge - Teacher Stats task 5
I'm stuck on task 5. I can get this dictionary to be converted to a list but having trouble replaceing the sublist with the length of the sublist.
def stats(teachers):
teacher_list = []
for i in teachers.items():
teacher_list += i
for j in teacher_list:
teacher_list.replace(len(teacher_list[j]))
return(teacher_list)
I tried another approach which drew from task 4 using "key, value" variables but without success
def stats(teachers):
teacher_list = []
for key, value in teachers.items():
teacher_list.append([key, len(teachers[value])])
return(teacher_list)
1 Answer
Steven Parker
231,236 PointsYou're second try is very close! But when you reference "teachers[value]
", you're attempting to use the value as a key. What you probably meant to do was "teachers[key]
", but that's not even needed since you already have "value
" and can use it directly.
teacher_list.append([key, len(value)])
eestsaid
1,311 Pointseestsaid
1,311 PointsThanks Steven. That fixed it. I saw the key, value approach in a few youtube videos and the like and though it works I don't fully understand the sequence of operations. Are either of the following the correct intuition?
Thanks again
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThe first description is more accurate. The keys and and sizes are being appended together inside a mini-list.