Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Sets Set Math

Daniel Smith
Daniel Smith
10,172 Points

I'm very lost on the collections set math problem.

I've read through the teacher documents and other peoples questions and I still dont understand what to do here... I know im supposed to use the set math operators but i don't know where and I dont know how to loop and get the key without using 'if topic in' statement. i feel like the iter operator would be useful but i dont understand where I would put it

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):
    keys=COURSES.keys()
    values=COURSES.values()
    key_set=[]
    for keys in COURSES:
        if topics in values:
            key_set.append(keys)
    return key_set

Remember that in for loops, best practice is for your first variable should be unique (relative to that particular for loop). You are trying to define it beforehand which is not as helpful. To achieve what I think you are aiming for, you could simply change the for loop to

for key in keys 

If we remember DRY, since you do not use your keys variable anywhere else in your function, you can simplify your code by removing

keys=COURSES.keys()

and further change your for loop to

for key in COURSES.keys()

Although, you are now looping through only the keys of each pair in the dict. You may want to rethink what you are looping through using the above logic.

1 Answer

Daniel Smith
Daniel Smith
10,172 Points

i figured it out thanks!