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

Einars Vilnis
Einars Vilnis
8,050 Points

Ireally dont get the last question in making function for list of lists

it shows me allways error and i dont even get it def stats(profile): max_teacher = [] for key in profile: max_teacher.append(key) for value in profile.values(): max_teacher.append(len(profile[key])) return max_teacher

teachers.py
# The dictionary will be something like:
dictst = {'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(teachers):
  max_count = 0 
  max_teacher = ''
  for key in teachers:
    class_count = len(teachers[key]) 
    if class_count > max_count:
      max_teacher = key                   #You have to reassign max_count to have the current
      max_count = class_count             #class count here
  return max_teacher
def num_teachers(teachers):
  count = 0
  for keys in teachers:
    count += 1
  return count

def stats(profile):
  max_teacher = []
  for key in profile:
    max_teacher.append(key)
  for value in profile.values():
    max_teacher.append(len(profile[key]))
  return max_teacher

2 Answers

Hi Einars,

Final Edit? :)

OK, so we need to return a list of lists in the format [name, number of classes]. The issue I see - and have compared against my working version, is that you have two for loops in the stats function. When the first for loop is done, max_teacher will be a list of teacher names ['Kenneth', 'Andrew', 'Joy'] etc. Then, after the second for loop runs, their number of classes are being appended to the end of max_teacher resulting in something like

['Kenneth', 'Andrew', 'Joy', 7, 5, 4]

but what we need is

[['Kenneth', 7], ['Andrew', 5], ['Joy', 4]]

We can do everything we need in just one for loop.

def stats(profile):
  max_teacher = []
  for key in profile:
    # create a list containing the teacher and their number of classes ['Kenneth', 7]
    # append this list to max_teacher
  return max_teacher

I'm trying to not give the exact answer here, but if you continue to have issues please let me know and I'll show you my working code.

Best Regards

Updated Answer

Einars Vilnis
Einars Vilnis
8,050 Points

thanks. will give it a shot i got Your idea