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

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

please help

i dont know whether i am on the right path, i dont really understand the task

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(dict, list):
    for item in list:
        try:
            for key in dict:
                return 

3 Answers

Donatas Ramanauskas
Donatas Ramanauskas
28,538 Points

You need to return the total, of how many items in list are also keys in dictionary, your function is checking if item is in a dict and just printing out the item:

''' def members(dct, lst): count = 0 for item in lst: if item in dct: # check if item in lst is a key in dct, if it is, increase count by 1 count += 1 return count '''

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

Hi Torsten, this is what i did and its still giving me a syntax error

def members(my_dict, my_list): for item in my_list: try: if item in my_dict: print (item)

Torsten Lundahl
Torsten Lundahl
2,570 Points

The task is to count how many of the items from my_list are also 'keys' in my_dict.

You're on the right path, but you don't really need two loops. You want to take each item from 'my_list' and check for it in 'my_dict'.

Try changing the second for-loop to an if-statement instead, using it to check for 'item' in 'my_dict'. I would also use a variable to count each time 'item' appear in 'my_dict'

I hope my explanation can get you on the right path! :)