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

Bob Amand
Bob Amand
2,639 Points

Teacher list and class number appear correct in 'Bummer' section. Why not clearing?

The third part of this question will not pass. Compared to others, the code looks correct. I have rechecked the punctuation and attempted to make the len(tdic[k]) an integer but had no impact. Any suggestions? Thanks.

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(tdic):
  maxn = 0
  prof = ""
  nlist = []
  for pro in tdic:
    nlist = tdic[pro]
    if len(nlist) > maxn:
      maxn = len(nlist)
      prof = pro
  return prof

def num_teachers(tdic):
  tn = len(tdic.keys())
  return tn

def stats(tdic):
  interlist = []
  totlist = []
  for td in tdic:
    interlist = td,len(tdic[td])
    totlist.append(interlist)
  return totlist

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hola,

Ok, So I get a dictionary. I need to put it in a list inside a list. This list must contain a name, and the number of classes that person teaches.

First thing is first. I need to create an empty list.

 I used x = []

I need to iterate, through a dictionary obtaining the key and the value. I can use a for loop for this. Note python27 can use iteritems() but python34 does not have that and requires us to use just items().

for key, value in tdict.items()

ok awesome. So now I need these two values to be put into a list.

tempList = [key, value]

Now, This appears to be right, but the directions say I want the NUMBER of classes. Which means value contains more than 1 value. Well, since I am in a list, I can use the length "len" attribute on value.

tempList = [key, len(value)]

Now all I have to add tempList to my other list I created which was x. So the Full code Looks like this...

def stats(tdic):
  x = []
  for key, value in tdic.items():
    tempList = [key, len(value)]
    x.append(temp)
  return x

Does that solve your issue? Please let me know if you still need help and what I can help you with.

Bob Amand
Bob Amand
2,639 Points

Thank you. Surprised that we don't have to declare the tempList, presumably because it is not a variable. I had also missed putting the key, len(value) into brackets!

Ryan Ruscett
Ryan Ruscett
23,309 Points

Heyyyy,

We can't declare tempList like we did with x.

See, x.append() only takes one item. It can't take two items.

>>> for key, value in tdict.items():
...     y.append(key, value)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: append() takes exactly one argument (2 given)

We can only pass append 1 item. So in order to make a single item. We have to create the list in a more literal form.

We create the list by saying

 tempList = [key, value]

Now I can append that to a list, since I am only adding 1 item instead of 2.

x.append(tempList)

That is why we don't declare the tempList the same as we do with x. But we still do declare the tempList. Just at a different scope and a different way.

does that make sense?