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

Stuck on Python Collections (New) // Sets - Challenge Task 2 of 2

Hello fellow Pythonistas,

I can't seem to wrap my head around this challenge. One other post has been made on the topic, but it wasn't really ever solved.

The first part of the challenge is still hazy for me. The "for keys, values" part specifically. A push in the right direction would be greatly appreciated! Thanks in advance.

  • Sam
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(single_parameter):
    empty_list = []
    for keys, values in COURSES.items():
        if single_parameter.union(values):
            empty_list.append(keys)
    return empty_list

def covers_all(single_parameter):
    new_empty_list = []
    for keys, values in COURSES.items():
        if single_parameter.intersection(values):
            new_empty_list.append(keys)
    return new_empty_list
Umesh Ravji
Umesh Ravji
42,386 Points

You're right about the len() method Sam, I don't think it's needed. I think I was just doing it that way so you could calculate the length once before the looping begins and then use that each time to compare to the length of the result rather than comparing the two sets.

Ismail KOÇ
Ismail KOÇ
1,748 Points

If you still have trouble with second step, check this link.

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Samuel,

You have everything correct, except for the operation being used on the set. I at first used intersection too, but then I remembered that Kenneth likes for us to use the docs to find new things that he didn't necessarily go over in the videos. So, if you go to the docs, you will find the operation needed to complete task two. I'll give you a hint... it's very very similar to intersection and will "Test whether every element in the set is in other." (That's a pretty big 'give away'. :smiley:)

Keep Coding! :dizzy:

Umesh Ravji
Umesh Ravji
42,386 Points

Thanks Jason, never even considered doing it that way, so much easier :)

Thank you Jason! I got it to pass using the method Umesh described. I do want to try the challenge again using the method you are alluding to. Based on the docs, I'm guessing you are talking about issuperset.

Umesh Ravji
Umesh Ravji
42,386 Points

I found this one a little tricky too.

You can use the intersection method on sets to make a set of elements that exist in both sets. So you can use this intersection on the input set and the test set to find which test sets result in a set that has the same amount of items as the input set, in this case 2.

if len(input_set.intersection(test_set)) == 2: # len(input_set)
    results_list.append(key)

Umesh to the rescue!

So basically, intersection is searching for elements in Courses that are the same as whatever the input is. I grasp that part.

I guess I got hung up on the fact we had to use len() for the values. Still a little confusing, but at least I know the solution and can keep looking into it until it clicks.

Thanks for the help!

Hasan Ahmad
Hasan Ahmad
6,727 Points

This method is not working for me. It says that task one is no longer passing

def covers(single_parameter):
    empty_list = []
    for keys, values in COURSES.items():
        if single_parameter.union(values):
            empty_list.append(keys)
    return empty_list

def covers_all(single_parameter):
    new_empty_list = []
    for keys, values in COURSES.items():
        if single_parameter.issuperset(values):
            new_empty_list.append(keys)
    return new_empty_list ```