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

stats

Hi Team,

I have two list. One with teachers name and other with counts of courses. Now I need to form a new list with pair of teachers name and courses counts list in it.

This is code. I need to add additional lines to it.

Final answer should be [["Kenneth", 2],["James",3]]

teachers_dict = {"Kenneth": ["Flask","Django"],"James": ["BD","Java","Dotnet"]} def stats(teachers_dict): t_j=[] t_c=[] t_t=[] for x in teachers_dict.keys(): t_j.append(x) print(t_j) for y in teachers_dict.values(): for z in y: t_c.append(z) t_t.append(len(t_c)) print(t_t) t_k=[] # Wrong code for x in t_j: t_k.append(x) for y in t_t: t_k.append(y) print(t_k) return t_k# stats(teachers_dict)

Please help. Thanks!

teachers.py
# 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(teachers_dict):
    count = 0
    for x in teachers_dict.keys():
        count+=1
    return count
def num_courses(teachers_dict):
    count_c=[]
    for courses_c in teachers_dict.values():
        for x in courses_c:
            count_c.append(x)
    return len(count_c)
def courses(teachers_dict):
    count_c=[]
    for courses_c in teachers_dict.values():
        for x in courses_c:
            count_c.append(x)
    return count_c
def most_courses(teachers_dict):
    most_course= 0
    for k,v in teachers_dict.items():
        #for value in teachers_dict.values():
        if len(v)> most_course:
            most_course=len(v)
            teacher = str(k)
    return teacher

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

This is how you solve the stats portion.

def stats(teacher_dict):
    stats_list = []
    for teacher_name, teacher_courses in teacher_dict.items():
        stats_list.append([teacher_name, len(teacher_courses)])
    return stats_list

Thanks Jeff Muday! Finally I completed the challenge!