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

need help am failing to get the required courses

.

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):
    return int(len(teachers))

def num_courses(tt):
    courses = 0
    for value in tt.values():
        courses += len(value)
    return courses

def courses(slist):
    for courses in slist.values():
        course =  list(courses)
        return course

1 Answer

Antonio De Rose
Antonio De Rose
20,884 Points

Not sure how that works, but I was able to get around that issue by doing the below

# the reason for me to have 2 for loops, was to have one flat list, if I stop from the first loop
# and then to append, I would have had a list of lists

#think of it like, the moment you have tt.values(), you result in something lie

# tt = [
# ['jQuery Basics', 'Node.js Basics'],
# ['Python Basics', 'Python Collections']
# ]

# when you loop for the first time, you have a list of lists
# value = ['jQuery Basics', 'Node.js Basics'],
# the above itself will not work, as it is another list
# cause, we are interested in the strings. will have to break till you have a string in hand
# hence loop over again, for the tx to have 'jQuery Basics', 'Node.js Basics'
# append it to courses, the newly created list, for the return
# it will come out of the inner loop, as it had finished the inner loop
# now go to the outer loop, have got the next one remaining, ['Python Basics', 'Python Collections']
# do same

Dict = {
        'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
        'Kenneth Love': ['Python Basics', 'Python Collections']
       }

def num_courses(tt):
    courses = []
    for value in tt.values():
        for tx in value:
            courses.append(tx)
    return courses

print(num_courses(Dict))