Python Forum

Full Version: Select the other of 2 items in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a bit of code that selects the other of two items in a list. It works fine but I'm wondering if there a more pythonic way of doing it
import random
pieces = ["X", "O"]
piece = random.choice(pieces)
print(piece)

# is there a better way to select the other item in the list
opponent = pieces
opponent.remove(piece)
opponent = opponent[0]

print(opponent)
What do you want to do, remove the piece from the existing list or get a new list that is the same as the existing but with the piece removed?
I would probably just shuffle the list, then use the first one as my piece and the second as the opponent.

import random
pieces = ["X", "O"]
pieces = random.shuffle(pieces)

# my piece is always pieces[0] and my opponent is pieces[1]
If you really prefer to use separate variables for each piece (and I think there are reasons you shouldn't) then you could just sample the list like this...

import random
pieces = ["X", "O"]
piece, opponent = random.sample(pieces, k=2)
(Aug-31-2020, 07:48 PM)bowlofred Wrote: [ -> ]I would probably just shuffle the list, then use the first one as my piece and the second as the opponent.

import random
pieces = ["X", "O"]
pieces = random.shuffle(pieces)

# my piece is always pieces[0] and my opponent is pieces[1]
If you really prefer to use separate variables for each piece (and I think there are reasons you shouldn't) then you could just sample the list like this...

import random
pieces = ["X", "O"]
piece, opponent = random.sample(pieces, k=2)

What I provided was just a simple working example. I need to be able to have one item of a list in a variable and use that yo put the other into a separate variable.
I cannot select them both at first as one item may be compared to many lists to select its counterpart from many list pairs.

opponent = pieces
opponent.remove(piece)
opponent = opponent[0]
Optomizing these 3 lines is my goal

(Aug-31-2020, 06:44 PM)deanhystad Wrote: [ -> ]What do you want to do, remove the piece from the existing list or get a new list that is the same as the existing but with the piece removed?

Not quite either. I have a list with two items and I have a variable containing one of those two items. I want to use the list and the variable to put the other item into a third variable.

Using a list and a variable containing one item in the list, I need to be able to store the other item in the list to a new variable.
In your attempt above, if these are really lists then the 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]
(Aug-31-2020, 08:51 PM)bowlofred Wrote: [ -> ]In your attempt above, if these are really lists then the 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]

You got it exactly. That is the answer that I was looking for. Thank you
(Aug-31-2020, 08:30 PM)Clunk_Head Wrote: [ -> ]I need to be able to have one item of a list in a variable and use that yo put the other into a separate variable.
I think the solution, provided bu bowlorfred, that shuffle pieces and then unpack in two separate variables does exactly that and is the best one
(Sep-01-2020, 04:09 PM)buran Wrote: [ -> ]
(Aug-31-2020, 08:30 PM)Clunk_Head Wrote: [ -> ]I need to be able to have one item of a list in a variable and use that yo put the other into a separate variable.
I think the solution, provided bu bowlorfred, that shuffle pieces and then unpack in two separate variables does exactly that and is the best one

Not really, as they are to be drawn in separate pieces. This was not made evident by my original question or example. His later answer of using list comprehension serves the purpose for which I intended this code. I replied as such and marked the thread as solved.