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 Out Of This Word Game

Can someone explain the line "if not symmetric_difference.?

I understand that symmetric difference is the set that is unique to both a & b with no overlap. Therefore by saying not symmetric difference what is the difference between that and simply calling the intersection between A and B

Andrew Voitsekhovskyy
Andrew Voitsekhovskyy
3,523 Points

I think that "not symmetric_difference" means sets don't have unique elements , so they equal to each other.

4 Answers

Hi David,

It's true that the symmetric difference is everything not in the intersection but checking if there's no symmetric difference is not the same as getting the intersection.

The logic in the code is checking if there is no symmetric difference. Meaning, the sets completely intersect.

If you get the intersection between the two, then you'll only know that at least some correct guesses have been made. You wouldn't know if they completely intersect and all the correct guesses have been made. Additional logic would be needed to make intersection work out.

I think another way you could approach the logic is to check if the 2 sets are equal.

if word_set == correct:
Chris Sehnert
Chris Sehnert
30,857 Points

Since "symmetric_difference" returns all of the items that are not in both sets... "if not symmetric_ difference" is only True if the sets are identical ....try this in the shell.... s1 = {1, 2, 3}

s2 = {1, 2, 3, 4}

if s1 ^ s2: print(True)

if not s1 ^ s2: print(True)

then make the sets identical and try again.....it's just another way of establishing a boolean value in a conditional....

more to the point.....the "empty set" returns False therefore, if the symmetric_difference is the empty set......it's False... soooo..... not symmetric_difference is True

Garrett Phipps
PLUS
Garrett Phipps
Courses Plus Student 2,808 Points

What helps me sometimes is constructing plain English sentences out of code. For instance:

if not word_set.symmetric_difference(correct):

would read in English as, "If the word set and the set of correct guesses are exactly the same..." while :

if word_set.intersection(correct):

would read as,"If the word set and the set of correct guesses have common values..."

Ra TePara
PLUS
Ra TePara
Courses Plus Student 1,319 Points

if not symmetric_difference(): is checking for a bool output.....If symetric_difference() returns a False do to an empty set(), which means they both have the same set values, then the not makes it a True which satisfy the condition to execute.