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

Set Math Challenge 1 trouble getting started

Will someone please give me a boost getting started on this? Start with an empty list to store a new list? Do I need to make separate sets of the dictionary keys and values then get the intersection of those? Or is it possible to go directly to the intersection of the keys and values of the COURSES set (which isn't that really a dictionary?)? Thank you!

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

The short answer is yes... either way can be used. It is more efficient to use Python's native set operations, but sometimes you need to construct your own set operations, like below.

def construct_intersection(s1, s2):
    """return the intersection of s1 and s2"""
    s3 = set()
    for x in s1:
        for y in s2:
            if x == y:
                s3.add(x)
    return s3

set1 = {'oats','peas','beans'}
set2 = {'lettuce','milk','oats'}

# Python native operational set intersection
print(set1.intersection(set2))

# a manually constructed intersection
print(construct_intersection(set1, set2))
set(['oats'])
set(['oats'])
Kayc M
Kayc M
12,593 Points

Thanks that was helpful.