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

Please help me with this python code

Hi, I need to match the list items and dictionary items and return the count but I can't figure it out, please can you help me.

# 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
my_list = ["app", "ora", "cab"]
my_dict = {"app": 1, "ora": 2, "bug": 3}

def members(my_dict, my_list):
  count = ()
  for key in (my_dict, my_list):
    if my_dict[:] == my_list[:]:
      count +1
return count

1 Answer

1) You are setting count to (), this isn't even a number. 2) only dictionaries have keys, so it doesn't make sense to look for them in my_list (you're also make a tuple of my_dict and my_list, and then iterating over it, probably not what you are trying to do 3) this syntax copies a list my_list[:], why copy it? why not just use it directly 4) count + 1 does not update any variable, any time you are updating a variable you'll be using the assignment operator = 5) your return is at the same level of indentation as def ....; this puts it outside the function, it should be inside the function

You may want to consider going back through the videos to pick up some more of the syntax.

Hi, thanks mate but I've done it now. I might just do as you said and start over if it gets too hard.