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

Kristian Vrgoc
Kristian Vrgoc
3,046 Points

teachers stat, challenge 1/5: num_teachers.

Hey Guys, @chrisfreeman2

I try to gain a better understanding. My questions are added as a comment in the script.

teachers.py
my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'Kenneth Love': ['Python Basics', 'Python Collections']}

                                    # I tried both ways.
def num_teachers(my_dict):          # Does the program automatically searches for the key or why is the .keys() not demanded.
    return len(my_dict)             # What would I have to type in as an argument to call the function in the workspace? I am a little bit confused.
                                    # The console shows me a NameError, because the argument is not named!?     
def num_teachers(my_dict):
    return len(my_dict.keys())

3 Answers

Eric M
Eric M
11,545 Points

Using len() in Python will call the object's __len__ method, which defines how the length of that object works.

For a dictionary, a key/value pair is considered 1 unit of length.

Let's look at a couple of dictionaries:

{"key_one": "value_one"}

That dictionary has a length of one, the key and the value are both strings.

{"key_one": ["list item one", "list item two"]}

The above dictionary has a length of one - one key and one value. The value is a list of length 2, but this does not change the length of the dictionary.

The length of a dictionary will be the same as the length of its keys. However the length of a dictionary alone does not tell you the size or complexity of the values within a dictionary.

For instance, I could have a dictionary that has other dictionaries as its values, all with varying lengths.

Either of the example functions you posted should pass part 1 of this challenge - because they will return the same integer value. You should not have two functions with the same name in the same scope of your program however, how will the Python interpreter know which function to use?

Cheo R
Cheo R
37,150 Points

By both ways, do you mean at the same time?

I tried your second approach, without explicitly declaring the dictionary (in your case my_dict) and it passed 1/5.

Kristian Vrgoc
Kristian Vrgoc
3,046 Points

Thanks, guys for your help! I completed the challenge and gained a better understanding. No more answers needed.