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 Membership

Kim Lam
PLUS
Kim Lam
Courses Plus Student 2,801 Points

What does this challenge ask me to do, and what is the comment trying to tell me? I don't understand at all. ):

This is the code I tried to use:

def members(dictionary, list):
    count = 0
    for item in dictionary[list]:
        count+=1
    return count

As you may notice, I have no idea what I'm doing. xD I did re-watch the previous video, Introduction to Dictionaries. Perhaps I am missing basic knowledge that was provided even before this video?

Specifically, I don't know what the "key in dict" syntax is, I'm not sure why dictionary and list of keys are separated into two arguments, and I don't know how to check if a key is a member of the dictionary. A lot of things, I know. >.< I've never had such a big issue with code challenge before, but maybe I'm just having a gigantic brain fart.

Any tips and advice would be appreciated!

P.S. Technically, I can skip the code challenge, since I completed it by adding the line of code

count -= 4

before returning the value, but I want to know the actual answer that was expected of me so I'm not missing valuable information I may need to know in the future. If anything, this might help if anyone is interested in not allowing users to cheat through this code challenge in the future, though I doubt it's worth the trouble.

Again, thank you for your time. I tried my best to be as specific as possible about what I'm confused about.

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Kim,

You had the general jist of what the task was expecting but aren't quite there so let's break it down, the task firsts asks us to create a members function, then we need to assign out parameters the first been the dict and the second been the list.

So far so good with your code with one problem, the keyword list is an reserved keyword in Python therefore it's bad practice to override it within the function, a better name would be keys since the challenge refers to them as that.

Next what we need to do is count the number of keys that exist within the dict which we can do by iterating over the list and using the in keyword to determine if the key exists or not, if it does we increment count like you're already doing.

The final step which you've also done is return the count so you're good in that regard too, the final code we should end up similar or the same as the below.

def members(items, keys):
  count = 0

  for key in keys:
    if key in items:
      count += 1

  return count

I'm not sure why dictionary and list of keys are separated into two arguments, and I don't know how to check if a key is a member of the dictionary.

The reason they're split up in the challenge is because we are searching for those specified keys within the dict, for example you could use this technique if you're filtering say a users details dict to retrieve only values you want.

P.S. Technically, I can skip the code challenge, since I completed it by adding the line of code

count -= 4

Deincremting the value of count shouldn't have let you pass the challenge as the count would have been less than 0.

Skip Gibson
Skip Gibson
5,417 Points

Sat scratching my head with this challenge for about 15 minutes, the way you have explained it has really helped it click for me. Thanks!

Kim Lam
PLUS
Kim Lam
Courses Plus Student 2,801 Points

Oh! I've been using list as a parameter/argument for a long time; I had no idea it was a reserved keyword! Python has been so lenient towards me, haha. Oh I see; we're checking to see if the dictionary has items from the list! It makes so much sense now. Thanks Chris!

The challenge told me that it received 6(I'm assuming that the dictionary had 6 items in it and my previous codes just counted all the items in the dictionary) when it was expecting 2 as feedback for my failed attempts, so I subtracted 4 from my count before returning it. As a result, I passed and got points for completing the code challenge xD It happened to me, really! I do agree it shouldn't have let me pass; I was just mentioning it in case anyone cared to know it was possible.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Naughty, naughty! :)

Ultimately, I'm just making sure that I get the right answer from your code. I don't check how you got that answer, most of the time, so you can get passes like this (you shouldn't try to, though).