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

selcuk baran
selcuk baran
3,526 Points

I revised it however still a problem

def members(dict,list): count=0 for item in list: if bool(dict[item])==True: count=count+1 else: count=count return count

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):
    count=0
    for item in list:
        if bool(dict[item])==True:
            count=count+1
        else:
            count=count
    return count

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

selcuk baran hi there.

The answer here might be simpler than you think.

def members(dic, ls):
    count = 0
    for item in ls:
        if item in dic:  # check if item is found in the dictionary keys
            count += 1   # if so, increment count by 1
    return count

I name my two function arguments dic & ls to avoid confusion with the build-in Python keywords dict & list.

Lemme know if you need more help. Cheers

jason chan
jason chan
31,009 Points
def members (my_dict, my_list):
    count = 2
    for temp in my_list:
        if my_dict in my_list:
            count += 2
    else:
        return count
selcuk baran
selcuk baran
3,526 Points

if you have time could you please explain a little bit?