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

Youssef Moustahib
Youssef Moustahib
7,779 Points

.difference does not work

Hi, I don't know why this Sint working:

a = {2,4,6,8} b = {1,2,3,4,5,6,7,8}

input: b.difference(a)

output: {1,3,5,7}

input: a.difference(b)

output: set()

Why do I keep getting set()?

2 Answers

Chels .
Chels .
4,720 Points

You're getting an empty set returned because .difference() shows what is different between the sets, in reference to the 1st set. a.difference() shows what values are unique to set a.

In b.difference(a), you're returned {1, 3, 5, 7} because those values are unique to b. They are not present in a.

When you call a.difference(b), it turns out there is nothing in set a that is not also in set b. Set a has values {2, 4, 6, 8}, which are also present in set b. Thus, you're being returned an empty set! Set a has no unique values when compared to set b.

Hope that helps!!

Steven Parker
Steven Parker
231,269 Points

That's an "empty set".

It shows that because "{}" would be an empty dictionary.