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<noob />
17,062 Pointsthe use of the items() method on part 4
I dont saw it covered on the video i saw alot of answers usuing this method:
def most_courses(teachers_info):
max_count = 0
for teacher, courses in teachers_info.items():
if len(courses) > max_count:
max_count = len(courses)
max_teacher = teacher
return max_teacher
Anyone can explain the items() method to me?
what this line means?
for teacher, courses in teachers_info.items():
from where this "teacher" var come from? its not supposed to be "courses in teachers.info.items()?, what this "teacher" stands for?
# 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 len(teachers)
def num_courses(courses):
number = []
for course in courses.values():
number += course
return len(number)
def courses(num_courses):
list_courses = []
for x in num_courses.values():
list_courses.extend(x)
return list_courses
def most_courses(teachers_info):
max_count = 0
for teacher, courses in teachers_info.items():
if len(courses) > max_count:
max_count = len(courses)
max_teacher = teacher
return max_teacher
1 Answer
Steven Parker
231,269 PointsThe "items" method allows you to get the key and value at the same time as you iterate a dictionary. So in this case, "teacher" would hold the teacher's name (the key), and "courses" would hold the list of courses associated with that teacher.
For more information, see the chapter on Looping Techniques in the Python documentation.