removed_card = random.sample(deck, 5)
This is allowing me to access the values in the list, but it will leave them in the list. How do I make it where I can remove multiple items from the list at once?
This probably isnt the best way, but you can instead pick 5 indices inside of your list and pop those indices:
removed_cards = []
for _ in range(5):
removed_cards.append(deck.pop(randint(len(deck))))
list.pop(i)
removes the item at index i
. You can't do a list comprehension this way since deck.pop
will be changing the length of deck
which would cause index errors.
You have to use the remove()
function and remove it from the list like so:
import random
cards = random.sample(deck, 5)
cards.remove(deck)
I can't be sure if it works because I don't know what 'deck' is.