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

Write a function named courses that takes the dictionary of teachers. It should return a list of all of the courses...

Can anyone provide a hint regarding part 4 of challenge? I'm having trouble looping through dict to separate courses from the teachers.

At first I tried:

def courses(teachers): 
  total_courses = [] 
  total_courses.append(teachers.values()) 
  return total_courses

This returns an error (paraphrased): "only find 1 item in list, should be 18"

Since the values (ie the courses) are another list, I assume you need loop through and use pop() or unpack the dict from the beginning. Here's what I've tried, but still no luck:

def courses():
  total_courses = []
  final_list = []
  count = 0
  for x in teachers:
    total_courses.append(teachers[x])
  while count <= len(total_courses:
      print(total_courses)
      z = total_courses.pop(0)
      final_list.append(z)
      count += 1
    else:
      final_list.append(total_courses.pop(0))
      break
  print(final_list)

courses()

Anyways, thanks for your help.

Paulo Aguiar
Paulo Aguiar
14,120 Points

not sure if this is the issue, but on this line i noticed:

while count <= len(total_courses:

it should be :

while count <= len(total_courses):

14 Answers

boog690
boog690
8,987 Points

Hello:

The problem asks you to return a list of of all courses. Your function is not returning anything, it's printing. It should be returning the list with the list of all courses.

To get a list of all the courses, we'll need a nested for loop that'll (1st for loop) iterate through the dictionary and THEN (2nd for loop) iterate through the values for each key. These values will be the courses. Append these courses to an empty list (that you should initialize at the beginning) and RETURN that list (with the course names now in them).

I hope this helped. Best of luck and enjoy Python!

Ham Hamey
Ham Hamey
5,712 Points
def courses(teachers):
    output = []
    for courses in teachers.values():
        output +=courses


    return output
Jake Kobs
Jake Kobs
9,215 Points

I was so close lol. I tried appending the courses to a list and returning that, but I was returning a lists within a list. Thanks for this!

Mr. Ham, Thank you for this!! You saved a lot of my time.... Thank you so much!

Why do I get always 'It should be 18' error? I gave the dict myself. It is:

dct = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
'Kenneth Love': ['Python Basics', 'Python Collections']}

and my solution is this

def courses(d):
  return list(d.values())

So, what is wrong with this code?

Thanks.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You aren't actually using the dictionary you provide when the function runs. It uses the dictionary I give it as part of the validation test.

The reason you're getting the wrong answer, though, is that you're only combining the values as they are. For example:

>>> my_dict = {'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}
>>> my_list = list(my_dict.values())
>>> my_list
[[1, 2], [3, 4], [5, 6]]

How many members does my_list have? It only has 3. But, for the purpose of this function, I should be getting back [1, 2, 3, 4, 5, 6]. You have to combine the contents of each key's value into a new list.

def num_courses (teachers): x=0 for courses in teachers.values(): for co in courses: x+=1 return x

Thanks guys!

I finally got this to work (with some much needed outside help):

def courses(teachers):
  total_courses = []
  end_result = []
  for key in teachers.items():
    total_courses.append(teachers.values())
  for courses_of_one_teacher in teachers.values():
    for course in courses_of_one_teacher:
      end_result.append(course)
  return end_result

It seems too long for a simple task. If you have a more elegant solution, pls let me know!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

In the teachers dictionary, each key is a teacher and each value is a list of courses. You can get all of the values with .values(), as you've done. So why not just extend a new list with each of the .values()lists?

def courses(teachers):
    output = []
    for courses in teachers.values():
        # something!
    return output

I'll leave the "something" to you.

I don't think that the first for loop is necessary

J llama
J llama
12,631 Points

terrible style. should delete this so ppl don't get the wrong idea bro

By saying I was giving the dict myself, I meant to say that I am actually giving a dict myself. It could have been any other dictionaries besides the ones that you gave for validation. But I chose to work with this dict, because it came handy to me.

But, when I run this code it is actually saying that 'this function returned 5 instead of 18'.I am looking for 5 as well! So where is that 18 came from?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The 18 is what the code challenge validator is looking for. When the CC is 'judged', your function is called with the dictionary I provide, and I check the contents against what I expect to get back. If the contents don't match what's expected, the task is failed.

The dictionary that your function is called with for validation has 5 keys with values. Your solution will always produce a list with as many members as there are keys in the dictionary. You just happened to test with a dictionary that has 5 items in its values. Try your code on your computer or in Workspaces. Call len(courses(dct)) and see what you get. It should give you back 2, instead of the 5 that you're expecting.

Final question :)

What I did not understand is where this dictionary came from? Clearly the one I give (with two teachers and 5 courses) is not executed by my function, right? It is not in the code?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Right. Your function is called by some code that you can't see and given a dictionary that you can't see. The one at the top of the file is just an example.

OK thanks! :)

Daire Ferguson
Daire Ferguson
1,592 Points

yeah i's stuck with it saying should have returned 18 but only returned 5.... everything has worked, but its just saying i need 18 courses when i only have 5???

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hey Daire! Sorry you're having trouble with the challenge. Can you share your code so we can work together to find the problem?

Grant Savage
Grant Savage
960 Points

you were most likely using newlist.append(value). which returns results ([class 1, class 2, class 3], [class 4, class 5], ...) when you really want (class 1, class 2, class 3, class 4, class 5). Use extend(value) instead of append(value) to achieve this.

I did that and it's working.

def courses(d):
  out = []
  for teacher in d:
    out.extend(d[teacher])
  return out
Jerehme Gayle
Jerehme Gayle
5,206 Points

If anyone is still having issues with this, one thing you might try if you are using .append() to add the values to your list. Change .append() to .extend(). This website http://thomas-cokelaer.info/blog/2011/03/post-2/ is pretty black and white on why.

Vishay Bhatia
Vishay Bhatia
10,185 Points

Can someone please explain why this doesn't work!

def courses(teachers): course_list = [] for value in teachers.values: course_list.append(value) return course_list

You're missing the () after the values in your for loop.

Hi, can someone explain how I can improve on this code? I know I'm very close but I can't seem to complete the missing point. INPUT:

def courses(something): my_list = [] for courses in something: my_list.extend(something[courses]) print( my_list)

OUTPUT:

['Python', 'Java', 'J', 'a', 'v', 'a']

I meant to say return(my_list) in my INPUT. Sorry about that.

I went with this which worked for me. Wouldn't account for redundant classes though.

def courses(dict_teachers): courseList = [] for teacher in dict_teachers: for course in dict_teachers[teacher]: courseList.append(course) return courseList

Hie rd23 i tried your code and its not working on my computer, what am i doing wrong?