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

Members

def members(my_dict, my_list): count = 0 for i in my_list: if i in my_dict: count += 1 return count

whats wrong with my code?

2 Answers

Nothing's wrong with your code - except perhaps your formatting? It passes the Challenge when I format as below (whitespace is the only thing changed).

def members(my_dict, my_list):
    count = 0
    for i in my_list:
        if i in my_dict:
            count += 1 
    return count

assuming you have correct indents,

when you do "if i in my_dict" you are looking through the keys in the dict if you mean to look through the values I would try "if i in my_dict.values()"

consider this example

my_d = { "name": "alex", "permissions": "root", "password": "xyzzy" }

"alex" in my_d

False

"name" in my_d

True

And looking through values

"alex" in my_d.values()

True

The Challenge that Thomas wrote his code for is asking for the list items which match the dictionary keys, so his code is correct - unless you were just letting him know how to run through the values if he wanted to do that in another project.