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

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I am stumped

Maybe I am not cut out for programming. Please show me the answer to this question. I get that I have created a list of lists, but cannot figure out how to unpack them so that I have a single list of classes.

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.
# The dictionary will be something like:

my_dict = {'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):
  largest = 0
  for key in my_dict:
    if len(my_dict[key]) > largest:
      largest = len(my_dict[key])
      busy_teacher = key
  #print(busy_teacher)
  return busy_teacher  
most_classes(my_dict)

def num_teachers(my_dict):
  number = 0
  for key in my_dict:
    number += 1
  return number
num_teachers(my_dict)

# looking to create a list of lists in the format [<name>,<number of classes>]
my_list = ['name',0]
list_of_lists = []
def stats(my_dict):
  for key in my_dict:
    my_list[0] = key
    my_list[1] = len(my_dict[key])
    list_of_lists.append(my_list[:])
  return list_of_lists  
stats(my_dict)
#print("list of lists=",list_of_lists)
#print("dict =",my_dict)

# looking to creat a list of classes that all of the teachers teach.
def courses(my_dict):
  course_list = []
  for key in my_dict:  
    course_list.insert(0,my_dict[key])
  return course_list
courses(my_dict)
Elizabeth McInerney
Elizabeth McInerney
3,175 Points

My question is just about the last 5 lines of code. Everything else works.

4 Answers

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

Maybe I am not cut out for programming. Please show me the answer to this question.

Okay, Elizabeth, if I were to write the code myself, it'd probably look sth like this.

def courses(my_dict):
  course_list = []
  for key in my_dict:
    course_list += my_dict[key]
  return course_list

or

def courses(my_dict):
  course_list = []
  for key in my_dict:
    course_list.extend(my_dict[key])
  return course_list

Let me know if you have trouble understanding the code.

Kevin VanderWulp
Kevin VanderWulp
5,180 Points

It looks like you are trying to insert every key from the dictionary into index 0 of the list. I am pretty sure this is your problem. Try replacing course_list.insert(0,my_dict[key]) with course_list.append(my_dict[key]). This will add each key at the end of the list instead of inserting them at an index.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I had tried that first, and it also did not work. Append creates a list containing separate lists of courses. I think the program wants to see all of the courses in a single list. That's why I tried insert. But that just inserts the list into the list. I think I need to break up the individual lists before putting them all back together, but I don't know how.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I will try this, but I don't think it will work. The dictionary (my_dict) contains the names of teachers for the keys, followed by a list of courses for each teacher. One teacher has 2 courses, the other has 3. The things I tried, plus your code above (I am guessing) all will return a course list containing 2 lists, one for the first teacher and 1 for the second. I think the assignment is asking for a single list with all 5 classes shown in a row. The lesson prior to this assignment talked about how to iterate over the values in a dictionary, so I am guessing that the answer should incorporate that. Thanks so much for your continued help!