Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,362 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