Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

victor escarcega
victor escarcega
7,573 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,118 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(),[])