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

Gregory Heard
Gregory Heard
1,975 Points

Dictionary comparison challenge

Hi, this code works in my workspace pefectly fine. But in the code challenge it says it returns 1 when it should return 2.

Any help i greatly appreciated.

Thanks

counts.py
def members (dictionary, keys):
  common = 0
  for keys in dictionary:
    common += 1
    return(common)

2 Answers

Gregory, you need to compare the keys in one list with the keys in the other:

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

Here we take each key in my_dict, and if it's also in my_list then we count it.

Gregory Heard
Gregory Heard
1,975 Points

Hi,

Thanks for the speedy response. All makes sense now!