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

Covers_all 2/2

Not entirely sure what's not working. If you write code here please explain line by line.

Cheers

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(arg):
    hold = []
    for key, value in COURSES.items():
        if value & arg:
            hold.append(key)
    return hold

def covers_all(arg):
    new_list = []
    for k, v in COURSES.items():
        if COURSES[k] & arg:
            new_list.extend(k)
    return new_list

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Christopher,

you are very close. Your if statement is not complete and you should use append instead of extend.

def covers_all(arg):
    new_list = []
    for k, v in COURSES.items():
        # intersection command will create a list 
        # with the number of courses that overlap
        # so we can use len to extract this number
        # and compare it to the length of the parameter
        # if both sets contain the same number of topics
        # append the key (name of the course) to new_list
        if len(COURSES[k] & arg) == len(arg):
            # append will take the entire key (course name) and
            # append it at the end of new_list
            # extend will take every character from the key and 
            # append it to the new_list
            new_list.append(k) 
    return new_list

append() vs extend()

>> list = []
>>> list.append("Python Basics")
>>> list
['Python Basics']
>>> list.extend("Python Basics")
>>> list
['Python Basics', 'P', 'y', 't', 'h', 'o', 'n', ' ', 'B', 'a', 's', 'i', 'c', 's']

Does it make sense?

I appreciate the examples and explanation very much. Second time I've confused extend for append. Oddly enough it was the other way around last time! Much appreciated for the great walk-through.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Christopher, you are very welcome. Keep on coding!