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

Anders Axelsen
Anders Axelsen
3,471 Points

Task 1: How do I convert the keys in counts from strings to ints?

I'm not sure, if I have both the names and the fields placed in counts. If so, how do I seperate, before making them into ints?

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(dictionary):
    counts = []
    teachers = [{"Name": "Kenneth", "Field": "Python"}]
    for key in teachers:
        counts.append(len(teachers[key]))
    return counts

Posting the script here, makes me reconsider the way I have organized teachers. The green color sparks the fire in new ideas.

3 Answers

Steven Parker
Steven Parker
231,248 Points

A "new idea" would be good. Here's a few hints:

  • don't create your own list of teachers, you'll get them from the dictionary passed in
  • the instructions say your "function should return an integer" (not a list)
  • you won't need a loop, just return the number of teachers (records) in the dictionary
Anders Axelsen
Anders Axelsen
3,471 Points

Thank you for the reply, Steven. :)

So - where I'm at, I've learned,

  • That the parenthesis houses the argument, and the comma seperates number and quality of arguments.
  • That I should make the function return an integer. - I know what an integer is. I know what the return command does.
  • That I should delete my list, Teachers, as it will - when I know how to - return the key as an integer.

I think, I will run the dictionary course through, until I get a better comprehension of the syntax.

Steven Parker
Steven Parker
231,248 Points

Good so far! Here's another hint: Like you can do with a list, you can use the len() function on a dictionary to get the number of items in it.

Herman Brummer
Herman Brummer
6,414 Points
def num_teachers(dictionary_list):
    teacher_list = dictionary_list.keys()
    number_of_teachers = 0

    for teachers in teacher_list:
        number_of_teachers += 1
    return number_of_teachers
Herman Brummer
Herman Brummer
6,414 Points
def num_teachers(teachers_courses):
    return len(teachers_courses)
Steven Parker
Steven Parker
231,248 Points

Right, but I was going to let folks work it out from the hints.

Besides, Treehouse discourages the posting of explicit solutions without explanations.

Herman Brummer
Herman Brummer
6,414 Points

Sry Steven did not mean to step on your toes, I was not aware of this,

I think they might have solved it ages ago since it was a month ago.

Steven Parker
Steven Parker
231,248 Points

I imagine Anders has moved on long ago, I was just thinking about other students who might read this now and in the future when they are at the same point in the course. You might consider deleting the "spoiler" for their benefit.