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!

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

victor E
victor E
17,741 Points

How can I count the items in each list?

the second part is returning 2 lists, I cant seem to use len because its not counting each individual item in each list. How can I count the total items since it returns 2 lists, one with 2 classes each?

# 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(dictionary):
    return int(len(dictionary.keys()))
def num_courses(dictionary):
    return int(len(dictionary.values())

2 Answers

Daniel Turato
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

When you do dictionary.values() on the given dictionary in the challenge, it will return a list of lists and the length of that list is 2 as there are 2 lists of courses which is incorrect. So you would have to do something like this so you can reach into both sub lists:

def num_courses(dictionary):
    num = 0
    for courses in dictionary.values():
        num += len(courses)
    return num

a bit simple but this did it for me:

def num_courses(a):
  b= sum(a.values(),[])