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 trialvictor E
19,146 PointsHow 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
Java Web Development Techdegree Graduate 30,124 PointsWhen 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
Julian Ramirez
1,371 Pointsa bit simple but this did it for me:
def num_courses(a):
b= sum(a.values(),[])
print(len(b))