In your attempt above, if these are really lists then the
If so, I'd use a comprehension to pull out everything that doesn't match. This doesn't check that there are really only two elements, or that
opponent = pieces
isn't useful because it's just referencing the pieces
list, so any modification will modify both. I'm assuming that pieces
already exists and that you don't want to change it.If so, I'd use a comprehension to pull out everything that doesn't match. This doesn't check that there are really only two elements, or that
piece
is present as one of the elements. But if both are true, it will return the other element.opponent = [x for x in pieces if x != piece][0]