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

Andras Andras
Andras Andras
7,941 Points

I need some help with Sets.py

I used the script below however when I tried in Python shell it gave me an empty list but Treehouse has accepted. . Can anyone give me advice ? https://www.python.org/shell/

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(param):
    returnable = []
    for course in COURSES:
        if COURSES[course] & param:
            returnable.append(course)
    return returnable

1 Answer

Hi there,

Works fine for me (good job!)... how are you passing the set into the function? Are you calling set() on the string to search for? I'm sorry if that's not it, it's just the only thing I can think of other than typo's or case sensativity... if it doesn't apply, just skip this! lol.

To explain what I mean, say I wanted to pass "Python" into the covers() function. covers() expects a set, so I have to convert the string "Python" to a set:

If I pass in "Python" like this:

course = covers({"python"})
print(course)

It'll return the correct output, however if I typed it like this:

course = covers(set("Python"))
print(course)

I'd get an empty list. The reason is the set() takes an iterable object and turns it into a set. A string is an iterable, so set("python") would result in something like this:

>>> set("Python")
>>> {'t', 'P', 'n', 'o', 'y', 'h'}

And of course, none of those strings match any strings in the COURSES values, so the list is empty. To get a set that ends up as {"Python"} you can do one of the following:

covers({"Python"})    # Just write the set literal
covers(set(["Python"]))    # Wrapped in a list
covers(set(("python",)))    # Wrapped in a tuple, note the comma

You can even pass a dictionary to set() and get a dictionary of the keys... although as keys are already unique that's usually kind of pointless, but again, could be useful if you wanted to use set methods on dictionary keys :)

Hope it helps!

Andras Andras
Andras Andras
7,941 Points

I got it. Thanks Jon. My code included this conversation inside the function that's why I was confused. Now it works with this one as well.

def covers(param): returnable = [] for course in COURSES: if COURSES[course] & param: returnable.append(course) return returnable

var = covers({"Python"}) print (var)