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

Create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value i

dict1 = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
  'Kenneth Love': ['Python Basics', 'Python Collections']}
def most_classes(dict1):
  dict1_keys = dict1.keys()
  dict1_values = dict1.values()
  dict1_list_name = []
  dict1_list_value = []
  dict1_name_value = []
  for i in dict1_keys:
    print(i,':')
    dict1_list_name.append(i)
    for j in dict1[i]:
      print(j)
      dict1_list_value.append(j)
  a_1 =str(dict1_list_name[0])+':'+str(dict1_list_name[0])
  a_2 = str(dict1_list_name[1])+':'+str(dict1_list_name[1])
  return a_1+a_2
most_classes(dict1)

this is what I write for this program, I still work well on my python3.4 in my computer, but the website said it make some wrong. I don't know why even I could test well on my computer but not good for the website. I think they need to improve in that.

[MOD: added ```python markdown formatting -cf]

6 Answers

thank you for your patient respect, and each time your code is more thinner and more clean than my code, I will learn and think about your code. Thank you very much to teach.

Nikhil Rai
Nikhil Rai
15,391 Points

I copied your code to the workspace and tried playing with it. At teh end I ended up changing the whole code. Please have a look:

def most_classes(dict1):
  teacher = "" 
  classes = []
  for tcher in dict1.keys():
    for entry in dict1.values():
      lst1 =  entry
      if len(lst1) > len(classes):
        classes = lst1
        teacher = tcher

  return teacher

You just need to check which value has the maximum length and return the corresponding key.

I am not a pro yet, but i would suggest try to keep your code precise and clean so that it is understandable(good practice to follow).

Also, I would like to notify that, here in the code challenge, we are supposed to just write the code that can perform the task (IN OUR CASE: return the NAME of teacher who has maximum number of classes ). However I noticed you are returning an proper string with EXTRA info. We don't need that for the task (being precise with what is needed!).

Hope it helped. Cheers :)

Thank you very much, u give me a big hand, yes I didnโ€™t make clear the test ask, thank u for your help

Nikhil Rai
Nikhil Rai
15,391 Points

If this answered your question ... I would be glad if you could upvote it or rate is as your answer ... so that other students can point it out easily. cheers :) keep learning.!!

yes, I change my code and work well, but I have to say your code is more clean and more short than my code. I don't really understand this code: for tcher in dict1.keys(): for entry in dict1.values(): I know you use two loop, but I still not very clear what they really meaning in here, thank you.

Nikhil Rai
Nikhil Rai
15,391 Points

Hey hello lion, sorry for late response, after you pointed it out ... I noticed that although it solved the given problem, it was not the absolute answer. (Thanks for pointing it out!!)

I tried it in even simpler way, check this out:

def most_classes(dict1):
  teacher = "" 
  classes = []
  for tcher,subject in dict1.items():
    if len(subject) > len(classes):
        classes = subject
        teacher = tcher

  return teacher

I dont know why, it is showing "quot;"" however is it empty string teacher = "". Cheers!

Hello, I don't know if you have use nltk before, because right now, when I want to install in on my windows64 system ,and install python3.4. I have search from google, although they have many answer but don't have very clear answer which could easily process. If you know please tell me, thank you very much.