-2
import random

# Deck
Deck = ['1♤', '2♤', '3♤', '4♤', '5♤', '6♤', '7♤', '8♤', '9♤', 'J♤', 'Q♤', 'K♤', 'A♤', '1♧', '2♧', '3♧', '4♧', '5♧', '6♧', '7♧', '8♧', '9♧', 'J♧', 'Q♧', 'K♧', 'A♧', '1♡', '2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', 'J♡', 'Q♡', 'K♡', 'A♡', '1♢', '2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', 'J♢', 'Q♢', 'K♢', 'A♢']

# Drawing players hand 

for i in range(52):
 Card0 = random.choice(Deck)
 Deck.remove[Card0]
 Card1 = random.choice(Deck)
 Deck.remove[Card1]
 Card2 = random.choice(Deck)
 Deck.remove[Card2]
 Card3 = random.choice(Deck)
 Deck.remove[Card3]
 Card4 = random.choice(Deck)
 Deck.remove[Card4]

 print (Card0, Card1, Card2, Card3, Card4)

How would I remove this variable from this list the code above does not work. I am using del does this work. Am I doing something wrong?

  • 1
    Use `random.sample` instead of making repeated `choice`s. – Thierry Lathuille Sep 17 '21 at 16:29
  • 1
    You're not using `del`. – Barmar Sep 17 '21 at 16:30
  • You have typos: `Deck.remove[Card0]` should be `Deck.remove(Card0)` – Barmar Sep 17 '21 at 16:33
  • 1
    In addition to what was pointed out already, `Deck.remove[Card0]` is invalid syntax. `remove` is a method, meaning you have to use parentheses and not brackets, like `Deck.remove(Card0)`. Brackets are just for indexing a collection if items. – Random Davis Sep 17 '21 at 16:33
  • 2
    You need to change to `range(10)`. You're removing 5 cards each time through the loop, so you'll run out of cards after making 10 hands. – Barmar Sep 17 '21 at 16:35
  • Does this answer your question? [Is there a simple way to delete a list element by value?](https://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value) – Commander Tvis Sep 18 '21 at 12:15

1 Answers1

0

Removing an item from a list is inefficient, but since it appears all items are unique, you could use Deck.remove (with (parentheses), not [brackets] ).

As commenters noted, you also can't remove FOUR cards FIFTY-TWO times, because that's 208 cards removed from your 52-card deck. You want to loop N times instead of 52, where N is the number of hands you want. You could also accumulate each player's hand into a list (using list.append) by looping M times over a single random.choice and Deck.remove, where M is the number of cards you want.

Since you don't care about modifying the list, you could do a variety of things:

  1. Use random.shuffle to sort the list randomly, then draw as many as you like by doing cards = Deck[:how_many_cards_I_want]
  2. Since you're only selecting a small number, you can pick a random number R between 1 and N, then swap the position of the item at R with the item at N, then decrease N. This will prevent you from getting the same item twice. This algorithm is more efficient than (1) for drawing a small number of cards, and less efficient than (1) when drawing a larger number.

Example for 2:

def draw_cards(cards, count)
    # cards = [ ... ]
    # count = 4

    unselected_card_count = len(items)
    selected_cards = []
    for i in range(count):
        selection = random.choice(range(unselected_card_count ))
        selected_cards.append(cards[selection])
        
        # Swap
        cards[selection], cards[unselected_card_count - 1] = cards[unselected_card_count - 1], cards[selection]
        
        unselected_card_count -= 1
    
    return selected_cards
Casey Kuball
  • 7,717
  • 5
  • 38
  • 70