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

dictionaries

where am i gettign wrong

counts.py
# You can check for dictionary membership using the
# "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(word,theKey):
  return len(word)

1 Answer

Thomas Kirk
Thomas Kirk
5,246 Points

Hello,

Your code is only returning the total number of items in your dictionary. The question asks you to return the number of items in your list of keys that can be found in that dictionary.

To do so, you should loop through each item in the list, and check if it can be found in the keys of the dictionary. If the list item is in the dictionary keys, add 1 to a running total, and return that total after the for loop.

Hope that makes sense...

def members(my_dict, my_list):
    count = 0                        # set a variable, count, as 0, to count number of matches
    for item in my_list:             # for each item in the list of keys...
        if item in my_dict.keys():   # ...check if that item is in the dictionary keys.
            count += 1               # if True, add 1 to the variable count
    return count                     # return count, to give the number of matches