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 (2016, retired 2019) Sets Set Math

1,311 Points

code challenge sets.py task 1

I don't understand what the question is asking to kick this one off. The part of the question that doesn't make sense reads:

So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap.

For example, covers({"Python"}) would return ["Python Basics"].

Is the example suggesting that a random set is provided (here, covers({"Python"})) and out of the dict keys the only one that it overlaps with is ["Python Basics"]??? So if the random set that was provided was covers({"Python"}, {"Ruby"}, {"Monkeys"}) the list retrurned would be ["Python Basics", "Ruby Basics"]?

Robin Goyal
Robin Goyal
4,582 Points

Yes, your intuition is correct here!

3 Answers

The goal here is to give a set as an argument which contains elements in the the other sets to see if they common elements. You didnt understand the question correctly, you are trying to use intersection() between the keys of the topics set to the keys of the COURSES set which is wrong because u need to do the intersection() with the values of the COURSES set(which is a set) to the topics set, in order to append the keys of the COURSES set accrording to the question.

take a look at this code:

def covers(topics):
    #i create an empty list
    set_keys = []
    #i loop through the key,value pairs of each dict in the COURSES set in order to get access to the key and the value of each dict.
    for key,value in COURSES.items()
     #then i check if topics set and value of each key(each value of each key is a set) have common elements.
      if topics.intersection(value):
       #if they have common elements then i append the key of each key,value pair that we iterated on and found that they something in common
   #then i return the list
    return set_keys

if u try to run the program by calling the function:


u get the keys of key,value pairs that has common elements

treehouse:~/workspace$ python try.py                                                              
['Python Basics']                                                                                 
['Python Basics', 'Java Basics', 'PHP Basics']                                                    

I hope u understand this, i tried to explain it as good as i could

1,311 Points

Thanks bot.net. Ive been away and will have a look at this again now. Will let you know how I go. I appreciate your responses.

1,311 Points

Yep - I was not undesrtanding the question correctly at all and it is clear now what the question was asking. I also appreciate the elegant solution. So thank you.

I have a couple of follow up questions.

To clarify - covers({"Python", "Java", "integers"}) passes in a set which then is compared to values which through the .items() method is a series of individual sets. Is that correct?

I guess I'm not sure how the .items() method is working in this instance. So my next question, when you run

for key,value in COURSES.items()

does the .items() method give back a tuple of the dict keys and a tuple of the dict values as a tuple of sets? For eample if I test the following function

def covers(topics):
    for key, value in COURSES.items():


the output I get is

Python Basics
{'exceptions', 'arrays', 'Python', 'input', 'loops', 'conditions', 'variables', 'booleans', 'strings', 'integers', 'functions', 'floats'}
Java Basics
{'exceptions', 'input', 'loops', 'variables', 'strings', 'booleans', 'integers', 'Java'}
PHP Basics
{'PHP', 'conditions', 'HTML', 'variables', 'strings', 'booleans', 'integers', 'floats'}
Ruby Basics
{'input', 'conditions', 'strings', 'Ruby', 'integers', 'functions', 'floats'}

Im not sure how to interpret the output. I hope this make sense. Pleae feel free to ask me to clarify.

Hi. here are the steps u need to do: 1.create an empty list since u want to return a list. loop through the key value pairs of the dict with the items() function. check if the passed in set have common element with the value of each key value pair, u do it with the intersection command, if the sets does have common elements u append the key of the iterated key value pair to the empty list. Lastly u need to return the list. gl!

1,311 Points

Thanks bot.net and Robin

Given my intuition for the question above I tried just making a list of the key values only (seemed tidier), changed that list to a set and then perform the intersection.

Unfortunately the result I get is a empty list []. Any thoughts on this approach and hints as to where it might failing?

def covers(topics):
    set_keys = []
    for key in COURSES.keys():
    set_keys = set(set_keys)
    set_keys = set_keys.intersection(topics)

covers({"Python", "Java"})

when u use items() u loop through each of the key value pairs. u have 4 keys and a 4 values which contain a set. that is why when u print key and value u get back the first key which is python basics and the value of that key which is his set.

1,311 Points

Thanks bot.net