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

Getting error of 'int' object is not iterable.

Trying to count the number of teachers that occurs in a dict. I was trying to turn key values into a list, count the number of times a name occurs in a list (ideally every teacher would only occur once.), and then sum the list of integers.

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(my_dict):
    my_list = []
    for key in my_dict:
        my_list.append(key)
        counted_list = my_list.count(key)
        total_integer = sum(counted_list)
    return total_integer

1 Answer

Keith Whatling
Keith Whatling
17,759 Points

There is a lot of code there, easy to write lots but the trick is to read the question. I have done it loads of times only to be left with one blooming line! :)

Kenneth is asking how many teachers there are. So the dict is a teachers name as the key then the values are a list of courses.

So thinking algorithmically how about you just return the length of the dict.

def num_teachers(teach_dict):
    return len(teach_dict)

Give me a shout if you get stuck on the next bit, towards the end of the challenge it gets a little tricky but try and keep it simple.