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

Kevin Hower
Kevin Hower
14,920 Points

can't get code to work in challenge but it works on my local environment (python 3.4.2)

here is my code:

def members(dict, list): return len(dict)

mydict = {'name':'Malinda','hair':'brunette'} mylist = ['name','hair']

stuff = members(mydict,mylist) print(stuff)

When I enter this in sublime and run it in my terminal, it comes back with 2, but the challenge keeps saying "Expected 2, got 6". Where is it getting 6 from? Very confused :(

perhaps I'm missing something, but I honestly don't get why the list is even there. It seems superfluous; a red herring.

counts.py
# You can check for dictionary membership using the5
# "key in dict" syntax from lists.

### Example
# my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
# my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
# members(my_dict, my_list) => 2

def members(dict, list):
    return len(dict)

mydict = {'name':'Malinda','hair':'brunette'}
mylist = ['name','hair']

stuff = members(mydict,mylist)
print(stuff)

2 Answers

Gavin Ralston
Gavin Ralston
28,770 Points

A critical part of this task is the following part of the challenge:

Return a count of how many of the keys are in the dictionary

So you're going to want to use that list in your function, not simply return the length count of the dictionary.

You want to take the list, which includes the values you're looking for, then find out how many of those keys are actually IN that dictionary.

Hope that helps!

Kevin Hower
Kevin Hower
14,920 Points

Gavin, Thanks! I stepped away from it and came back and saw your message. It did help. Once I thought about it differently, it made sense and I was able to finish it. I just wasn't apparently in the right frame of mind to figure it out earlier. Kevin