Skip to content
Advertisement

Taking an element from a list/variable [duplicate]

I’m trying to take a card away from the deck after the player’s cards have been dealt, so those cards aren’t dealt again.

import random    
import time      


cards = ["Ace of Hearts", "Ace of Diamonds", "Ace of Clubs", "Ace of Spades", "King of Hearts", "King of Diamonds", "King of Clubs", "King of Spades", "Queen of Spades", "Queen of Hearts", "Queen of Clubs", "Queen of Diamonds", "Jack of Hearts", "Jack of Diamonds", "Jack of Clubs", "Jack of Spades", "Ten of Hearts", "Ten of Diamonds", "Ten of Clubs", "Ten of Spades", "Nine of Hearts", "Nine of Diamonds", "Nine of Clubs", "Nine of Spades", "Eight of Hearts", "Eight of Diamonds", "Eight of Clubs", "Eight of Spades", "Seven of Hearts", "Seven of Diamonds", "Seven of Clubs", "Seven of Spades", "Six of Hearts", "Six of Diamonds", "Six of Clubs", "Six of Spades", "Five of Hearts", "Five of Diamonds", "Five of Clubs", "Five of Spades", "Four of Hearts", "Four of Diamonds", "Four of Clubs", "Four of Spades", "Three of Hearts", "Three of Diamonds", "Three of Clubs", "Three of Spades", "Two of Hearts", "Two of Diamonds", "Two of Clubs", "Two of Spades"]

p1c = random.choice(cards)
cards = cards - p1c
p2c = random.choice(cards)
cards = cards - p2c

But here is my command error:

  File "<string>", line 26, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'str'

[Program finished]

Advertisement

Answer

I think you are looking for remove() which removes the element from the list:

cards.remove(p1c)
Advertisement