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

Arihant Tripathy
Arihant Tripathy
8,764 Points

I can't understand how to solve this challenge.

I get this error in the REPL - TypeError: unhashable type: 'set'

sets.py
COURSES = {
    "Python Basics": {"Python", "functions", "variables",
                      "booleans", "integers", "floats",
                      "arrays", "strings", "exceptions",
                      "conditions", "input", "loops"},
    "Java Basics": {"Java", "strings", "variables",
                    "input", "exceptions", "integers",
                    "booleans", "loops"},
    "PHP Basics": {"PHP", "variables", "conditions",
                   "integers", "floats", "strings",
                   "booleans", "HTML"},
    "Ruby Basics": {"Ruby", "strings", "floats",
                    "integers", "conditions",
                    "functions", "input"}
}

def covers(topics):
    if topics in COURSES:
        return list(topics)

So topics is coming in as a set. Courses is also a set so you can use the set math learned to find the intersection of those.

1 Answer

Steven Parker
Steven Parker
231,275 Points

You won't need to convert anything into a list, and as Kirsten points out both your argument and the dictionary values are sets, so you can compare sets using "set math". Any non-empty intersection would be considered an "overlap". When you get to task 2, you might use a subset instead.

Arihant Tripathy
Arihant Tripathy
8,764 Points
COURSES = {
    "Python Basics": {"Python", "functions", "variables",
                      "booleans", "integers", "floats",
                      "arrays", "strings", "exceptions",
                      "conditions", "input", "loops"},
    "Java Basics": {"Java", "strings", "variables",
                    "input", "exceptions", "integers",
                    "booleans", "loops"},
    "PHP Basics": {"PHP", "variables", "conditions",
                   "integers", "floats", "strings",
                   "booleans", "HTML"},
    "Ruby Basics": {"Ruby", "strings", "floats",
                    "integers", "conditions",
                    "functions", "input"}
}

def covers(topics):
    if COURSES.intersection(topics) == True:
        return topics

Now what's wrong?

Steven Parker
Steven Parker
231,275 Points

Here's a few additional hints:

  • this code returns the original argument unchanged
  • if the condition does not pass, nothing at all is returned
  • you'll need a loop to check the different sets in each dictionary entry (instead of the whole thing)
  • you'll need to construct a list of keys to return after the loop finishes
  • while not an error, you don't ever need to compare anything to "True" (just name it)