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

I believe I have completed the task but the expected output isn't clear. Here is the debug output I get from my program:

List Before f(x) call: {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'T echnology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections'], 'Ken neth Seifer': ['Python Basics', 'Python Collections'], 'Jason Love': ['Ruby Foundation s', 'Ruby on Rails Forms', 'Technology Foundations']}
Value in Dict:['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']
templist[0]:jason seifer
templist[1]:3
templist:['jason seifer', 3]
--before | olist:[]
--after | olist:[['jason seifer', 3]]

Completed loop #1

Value in Dict:['Python Basics', 'Python Collections']
templist[0]:kenneth love
templist[1]:2
templist:['jason seifer', 3, 'kenneth love', 2]
--before | olist:[['jason seifer', 3]]
--after | olist:[['jason seifer', 3], ['kenneth love', 2]]

Completed loop #2

Value in Dict:['Python Basics', 'Python Collections']
templist[0]:kenneth seifer
templist[1]:2
templist:['jason seifer', 3, 'kenneth love', 2, 'kenneth seifer', 2]
--before | olist:[['jason seifer', 3], ['kenneth love', 2]]
--after | olist:[['jason seifer', 3], ['kenneth love', 2], ['kenneth seifer', 2]]

Completed loop #3

Value in Dict:['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']
templist[0]:jason love
templist[1]:3
templist:['jason seifer', 3, 'kenneth love', 2, 'kenneth seifer', 2, 'jason love', 3] --before | olist:[['jason seifer', 3], ['kenneth love', 2], ['kenneth seifer', 2]]
--after | olist:[['jason seifer', 3], ['kenneth love', 2], ['kenneth seifer', 2], [' jason love', 3]]

Completed loop #4

mylist is: [['jason seifer', 3], ['kenneth love', 2], ['kenneth seifer', 2], ['jason l ove', 3]]

If I knew the expected output I'm sure I could correct this but I'm stuck and reaching for straws

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(idict):
  maxcount = 0

  x = 1
  ostr = ''
  for items in idict:
    print("{}. {}".format(x,items))
    print("{}. {}".format(x,idict.get(items)))
    tlist = idict.get(items)
    print("Templist: {}".format(tlist))
    print("Templist length: {}".format(len(tlist)))
    if len(tlist) > maxcount:
      maxcount = len(tlist)
      ostr = items
      print("New max found| Name:{}  Count:{}".format(ostr,maxcount))
    x += 1

  return ostr

def num_teachers(idict):
  count = 0
  for items in idict:
    count += 1

  return count

def stats(idict):
  olist = list()
  tlist = list()
  templist = []
  num = 0
  x = 0
  y = 0
  #print("entering loop")
  for items in idict:
    templist.insert(x,items.lower())
    x +=1
    tlist.extend(idict.get(items))
    print("Value in Dict:{}".format( tlist))      
    num = len(tlist)
    tlist.clear()
    templist.insert(x,num)
    print(" templist[0]:{}".format(templist[x-1]))
    print(" templist[1]:{}".format(templist[x]))
    print(" templist:{}".format(templist))
    print(" --before | olist:{}".format(olist))
    olist.append(templist[x-1:x+1])
    print(" --after  | olist:{}".format(olist))

    y +=1
    x +=1
    print("Completed loop #{}".format(y))
    print("------------------")
    print()

  return olist

1 Answer

hie Arif, i did it the simplest way, let take you through the challenge the way I understood it.

  1. I first defined my function stats, that takes a dictionary.
  2. I then created an empty list named final_list
  3. In order to get the number of classes for each teacher I used a for loop and stored this data in a variable I named sub_list.
  4. Using .append I then added this information to the variable final_list.

  5. Finally I returned final_list.

    def stats(idict):

    final_list = []
    
    for key, value in idict.items():
    
        sub_list = [key, int(len(value))]
    
        final_list.append(sub_list)
    
    return final_list