1
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?

1katwell
  • 13
  • 2

2 Answers2

0

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.

R Nar
  • 5,465
  • 1
  • 16
  • 32
-1

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.

Inkblot
  • 708
  • 2
  • 8
  • 19
  • `cards.remove(deck)` would not work. that is attempting to remove the original list from the sample that you get from `random.sample`. – R Nar Nov 30 '15 at 17:13