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 (2016, retired 2019) Dictionaries Teacher Stats

a a
a a
1,101 Points

Why len(dictionary.values()) cause TypeError?

I really stack on this Challenge Task so I need your help.

Question is this.

"Create a function named most_courses. most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable."

Example input is this.

"{'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}"

So Answer should be

'Andrew Chalkley', 'Kenneth Love'

So I make def most_courses like this. But it returns as TypeError 'int' object is not iterable.

def most_courses(arg_5):
    k=list(arg_5.keys())
    v=list(len(arg_5.values()))
    return k[v.index(max(v))]

I dont understand what makes an Error. I expect this one returning the list of [2, 2] because there are 2 values on each dictionary.

v=list(len(arg_5.values()))

I appreciate if you explain some hint or answer to this task.

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.

def num_teachers(arg):
    return len(arg)

def num_courses(arg_1):
  list1 = []
  for arg_2 in arg_1.values():
    list1.extend(arg_2)
  return (len(list1))

def courses(arg_3):
    list2 = []
    for arg_4 in arg_3.values():
        list2.extend(arg_4)
    return (list2)

def most_courses(arg_5):
    k=list(arg_5.keys())
    v=list(len(arg_5.values()))
    return k[v.index(max(v))]

1 Answer

This one tripped me up when I was going through this course. The main issue you are having is "I dont understand what makes an Error. I expect this one returning the list of [2, 2] because there are 2 values on each dictionary." You are correct in that you can see two items for each person. ['jQuery Basics', 'Node.js Basics'] for Andrew Chalkley and ['Python Basics', 'Python Collections'] for Kenneth Love. However the way .values() is working is not how you expect it to. What .values() is doing is it is getting all of the values for Andrew and all of the values for Kenneth and lumping them into two distinct lists. When you do .values() on the dictionary in teachers.py you get this result.

dict_values([['Python Basics', 'Python Collections'], ['jQuery Basics', 'Node.js Basics']])

When you run len(example_dictionary_from_teachers.values()) you are getting the result 2. (I did not use arg_5 because Kenneth said the dictionary would look something like the one above so he might be tricking you with a longer/shorter dictionary.) What is happening with the len() is len checks and finds [['Python Basics', 'Python Collections'], ['jQuery Basics', 'Node.js Basics']] being a single list. len then looks in the list and finds one item ['Python Basics', 'Python Collections'] then two items ['jQuery Basics', 'Node.js Basics'] so it returns a result of 2.

This messes up your list() in list(len(example_dictionary_from_teachers.values())) because you cannot turn the integer 2 into a list. Let us know if you have any more questions or need some clarification on anything.

a a
a a
1,101 Points

Thank you for quick and kind reply. I understand what you mean. And I understand I cant solve this problem by myself. ;(

If you have any idea let me know. Thank you again.

What do you mean by "And I understand I cant solve this problem by myself. ;("? Are you having more difficulties trying to solve the challenge? Don't worry if you are everyone of us needs help from others at times. Trust me I have gotten my fair share of help overtime. Just add a comment with any additional questions you may have.