How do I delete a list within a list in Python? -


my program involves drawing card deck, adding value of hand , deleting card deck (since don't want draw same card twice).

example of card:

card = ["4 of clubs", 4] 

the code doesn't work:

card_number = random.randrange(len(deck_cards)) card = deck_cards[card_number][0] print(card) hand_value += deck_cards[card_number][1] deck_cards.remove(card_number) 

i have no idea doing wrong, , if change code to

card_number = random.chioce(deck_cards) 

it won't accept card = deck_cards[card_number][0] part.

what do?

list.remove takes object, not index, parameter.

so should modify code retrieve card , use directly:

>>> card = random.choice(deck_cards) # retrieve object, not index >>> print (card) # card card list, i.e.: ["4 of clubs", 4] >>> print (card[0]) 4 of clubs >>> hand_value += card[1] >>> deck_cards.remove (card) 

note can delete object @ specified index using del built-in:

>>> card_number = random.randrange(len(deck_cards)) >>> card = deck_cards[card_number][0] >>> print(card) "4 of clubs" >>> hand_value += deck_cards[card_number][1] >>> del deck_cards[card_number] 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -