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 (Retired) Dictionaries Teacher Stats

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Returning 5 courses, not 18

Not sure what I am doing wrong here - is this something to do with .append(value) only appending unique courses, as opposed to going through everything? If not, not sure why I am getting stuck.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(my_dict):
  max_count = 0
  teacher = ""
  for key, value in my_dict.items():
    if len(value) > max_count:
      teacher = key
      max_count = len(value)
  return teacher

def num_teachers(teacher_dict):
  return len(teacher_dict)

def stats(t_dict):
  statslist=list()
  for key,value in t_dict.items():
    sublist=list()
    sublist.append(key)
    sublist.append(len(value))
    statslist.append(sublist)
  return statslist

def courses(t_dict):
  courselist=list()
  for key,value in t_dict.items():
    courselist.append(value)
  return courselist

So I am referring to the last part of that coding challenge.

And I thought the question was about returning only the list of courses taught by all the professors; basically the intersection of all the lists. But the solution that people have posted here returns all the courses taught by everyone thats like creating a huge list of courses (maybe even duplicate) and somehow thats the correct answer.

Do you guys think that the question was correctly framed?

William Li
William Li
Courses Plus Student 26,868 Points

Sanat Tripathi , you got a point, but the thought of checking for uniqueness never cross my mind, and here's why. I think of code challenge has a lot in common with test-driven development, except in this case the test suite has been written for me; and the thing about test-driven development is that you first try the simplest possible solution to get the test suite pass, if not, then you act according to the test feedback by keep adding things to make your code more sophisticated to handle edge cases and pass the test. In this case if the grade in fact demanding uniqueness in the return list, all I need to do is add one line if item not in courselist, and it'll pass. And there's big reason for doing that -- it's generally so much easier to improve a simple but imperfect solution and make it more complex than trying to simplify a faulted complicated solution; because once you get into the mindset of trying to approach a problem with the most complicated solution possible, and when that doesn't work, you basically stuck and unable to think about how to make the solution simpler.

3 Answers

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

That is very very helpful - kind of similiar I guess to the previous challenge where you need to create sublists to then put into the master list. Thanks a lot William!!

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Dhruv, the problem of your courses implementation is that it loop through the dictionary, and append each lookup value to the courselist list; But here is what's going wrong -- each value lookup only return you a list e.x. ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], you need to unpack the list first, then append each element of the list to courselist.

 def courses(t_dict):
  courselist=list()
  for key in t_dict:
    for item in t_dict[key]:
      courselist.appned(item)
  return courselist
def courses(adict):
    course_list = []
    for value in adict.values():
        for item in value:
          course_list.append(item)
    return(course_list)