BradTheBrutalitist BradTheBrutalitist - 6 months ago 37
Python Question

Python 3: How to move an item from list x to list y and remove it from list x?

I am trying to

.append
a random card from list
Deck
to
MyHand
, while removing it from
Deck
.

import random

Deck = []
MyHand = []
CardsPicked = 0

for Cards in range(1, 101):
Deck.append(Cards)

while(CardsPicked < 8):
MyHand.append(random.choice(Deck))
CardsPicked = CardsPicked + 1


THINGS TO KNOW: I've been able to add the card, but not remove it.

I tried doing
Deck.remove(random.choice)
, but it said that the choice wasn't in the deck.

Answer
MyHand.append(Deck.pop(random.choice(range(len(Deck)))))

(pop takes an index to remove and returns the item removed)

Something closer to your code would be:

card = random.choice(Deck)
Deck.remove(card)
MyHand.append(card)
Comments