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 Basic Object-Oriented Python Creating a Memory Game Game Class Part 1

Why do we convert this to a set?

self.locations.append(f'{column}{num}')

available_locations = set(self.locations) - set(used_locations)

My understanding is that locations is a string ex."D4". Why do we convert it to a set instead of just doing a direct comparison using strings?

1 Answer

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Hi Christopher! In the set_cards method, you're looping through each card option and assigning it a location (A2, A4, B2, etc.)

You don't want two cards with the same location. As you loop through, it's essential that you assign each card a location that hasn't been used already.

Using sets makes this easy to do in one line of code.

 available_locations = set(self.locations) - set(used_locations)

In each iteration of the loop, available_locations equals the difference of 2 sets: self.locations and self.used_locations.

Remember that in set terminology, calculating the "difference" between 2 sets returns everything BUT the intersection of the two sets (any elements present in both sets make up the "intersection.")

When we "use" a location, it will be in the set of used_locations on the next iteration of the loop. It will also be in the set of self.locations. Thus, it won't be in the set of available_locations- because, remember, available_locations calculates the difference between the self.locations set and the used_locations set.

I hope this helps! Let me know if that wasn't clear.