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

Rakshit H Ravishankar
Rakshit H Ravishankar
1,309 Points

Challenge 5 not working

def stats(dict1): list1=[] count=0 for i in dict1: list2=dict1[i] for j in list2: count+=1 list1.append([i,count]) return list1

Can anyone please tell me whats wrong with this logic. This code is not working

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(dict1):
    count=0
    for i in dict1:
        count+=1
    return count

def num_courses(dict1):
    count=0
    for i in dict1:
        list1=dict1[i]
        for i in list1:
            count+=1
    return count

def courses(dict1):
    list1=[]
    for i in dict1:
        list2=dict1[i]
        for j in list2:
            list1.append(j)
    return list1


def most_courses(dict1):
    d={}

    for i in dict1:
        list1=dict1[i]
        count=0
        for j in list1:
            count+=1
        d[i]=count
    max1=0
    for k in d:
        if d[k]>max1:
            max1=d[k]
            ele=k
    return ele

def stats(dict1):
    list1=[]
    count=0
    for i in dict1:
        list2=dict1[i]
        for j in list2:
            count+=1
        list1.append([i,count])
    return list1

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi there, if you haven't seen my post in your other thread, naming variables more appropriately can't hurt when coding. The problem is that count has been declared outside of the loop so it never starts at zero to count all the items inside the list, so you have to move it inside the loop.

Rather than loop over a list to count all the items, it's much easier to use the len() function.

def stats(dict1):
    list1=[]
    for i in dict1:
        count=0  # you want count in here
        list2=dict1[i]
        for j in list2:
            count+=1
        list1.append([i,count])
    return list1