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

I am having trouble counting the same elements in a list and dictionary as my code is printing the same?? kindly suggest

my_list = ['apples', 'coconuts', 'grapes', 'strawberries'] my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}

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

3 Answers

This code you have above is very close!!!! It seems you have actually reversed how you should compare the values. The easiest way to accomplish the goal would be to go through each dictionary key and check if it exists in the list. We do this by setting a variable "count" to 0 to keep track of the "my_dict" keys found in the list. We loop through each dictionary key, checking with the "if" condition for the key existence in "my_list". If we find the key in "my_list", increase the "count" by 1. Finally we print the "count" to find out how many "my_dict" keys were found in "my_list".

Example:

my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}

count = 0
for key in my_dict:
    if key in my_list:
        count += 1

print count

I also agree with Doug that a function is not needed for this unless you plan to reuse it over and over. If this is the case, make sure you always name your functions something notable as to what it will do (i.e. "countDictionaryKeysInList")

Hope this helps answer your question! Happy coding!! :)

thank u very much

I'm a little confused. Are you counting or printing? If counting, what are you supposed to be counting? If printing, what are you supposed to be printing? If you are just trying to check for and print the keys in the dictionary that share the items in the list, don't forget to call the function 'members' in your code. Perhaps you can copy and paste the question from the webpage?

Actually I am a in python just trying to count the items in the list that match with the dictionary

I'm a beginner learning Python. If this is for a class or you are trying to learn? Here is a hint, but no code solution. I try to not start coding, but break the question down into some steps that can be implemented in code or inform thinking: 1) Given a list. 2) Given a dictionary. 3a) How do I compare the elements in a list to the keys in a dictionary (element == key)? Maybe need to research this topic some more, based on the question to solve. 3b) Can I make the compare directly or do I need to convert my list to a dictionary or dictionary keys to a list? Maybe there is more than one option. 4) Given the solution to #3, test for matching items and make a set of the matches. 5) Count the matching items, print the result. PS I don't think you don't need to define a function to complete the solution.