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 trial

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

eestsaid
eestsaid
1,311 Points

Code 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
Steven Parker
231,236 Points

You'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
eestsaid
1,311 Points

Thanks 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?

  • from 'teachers' take the first key and value pair in the dict and append it to 'teacher_list' then repeat; or
  • from 'teachers' take all the keys successively in the dict and append to 'teacher_list' and then take values successively in the dict and append to 'teacher_list'

Thanks again

Steven Parker
Steven Parker
231,236 Points

The first description is more accurate. The keys and and sizes are being appended together inside a mini-list.