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

gibran erlangga
gibran erlangga
5,539 Points

number of items contained in list and dictionaries problem

guys, Im having a little confusion in this problem. I dont know what is gone wrong with my code. Anyone can help me out? thanks in advance guys!

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(my_dict, my_list):
    for my_dict in members:
        count = 0
        if my_dict == my_list:
            count += 1
        else count += 0
    print count

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I see a few problems with your code. The biggest one is that every single time through the for loop you reset your count to 0. So in your code, it's only possible to print a 1 or a 0. Secondly, you're looking for my_dict in members. But members isn't an iterable, it's the name of the function. Third, you're printing your result when the challenge explicitly states to return it. Take a look at how I did it :

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

I start the function by giving it the name members and telling it to accept two iterables. One is a dictionary and one is a list of keys. Then I set my count = 0. This line should only ever execute once inside the function. Now for every key in my_list I see if it exists inside my_dict. If it does, I increment count. After the for loop has finished, I return count. Hope this helps! :sparkles:

Good catch. I completely missed count being initialized inside the loop.

gibran erlangga
gibran erlangga
5,539 Points

Hello again Jennifer Nordell! Thanks for your thorough explanation about this.

Jennifer, I came across your answer and it is succinct as always. Wondering if you can tell me what I missed in my attempt at this challenge.

my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
# print(members(my_dict, my_list))
def members(my_dict, my_list):
    count = 0
    # for item in my_dict.keys():
    for item in my_dict: #tried this for and the one before it
        print(item)
        if item in my_list:
            count +=1
            return count

print(members(my_dict, my_list))
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there john larson! Short answer? Your return statement is in the wrong place. Remember that when we use the return statement the function/method immediately stops executing. So in your code that return statement belongs to the last if block. The return should happen after the for loop. If I take your code and copy/paste it and then move the return statement so that it lines up with your count declaration/initialization and the for loop, your code passes! Hope this clears things up! :sparkles:

Thanks for your explanation about the return statement. I get lost about where to indent things but that helps.

A dictionary is a set of key : value pairs in no particular order, so you can't iterate over the pairs in a dictionary exactly the same way you would iterate through a list. But, much like you can iterate over each item in a range, you can iterate over the set of all keys in a dictionary. To do this, you use:

for key in dict:
    # do something  

In this form, replace dict with the name of your dictionary and key variable to temporarily hold a value, so it can be any valid variable name that's not already in use in the code block.

In this particular case, you want to see how many keys in the my_dict dictionary are also in the my_list list. To do that you iterate through the keys in my_dict, checking each one to see if it is also in my_list. If the key is in my_list, you increment the counter by one.

for key in my_dict:
    if key in my_list:
        count += 1
gibran erlangga
gibran erlangga
5,539 Points

Hi Troy, thanks for your answer! really appreciate it.