Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an object from a list of objects

I basically have 3 classes. Card, Deck, and Player. The Deck is a list of cards. I am trying to remove a card from the deck. But I am getting a ValueError saying that the card is not in the list. From my understanding, it is and I am passing the correct object through the removeCard function. I am not sure why I am getting a ValueError. So in short, the problem is that I need to remove an object (Card) from a list of Cards.

My issue is that when I try to remove a card from the deck I get an error like this:

ValueError: list.remove(x): x not in list

This is what I have so far:

Card class:

import random

class Card(object):
    def __init__(self, number):
        self.number = number

Deck class (the error is thrown here, in the removeCard function):

class Deck(object):
    def __init__(self):
        self.cards = []
        for i in range(11):
            for j in range(i):
                self.cards.append(Card(i))

    def addCard(self, card):
        self.cards.append(card)

    def removeCard(self, card):
        self.cards.remove(card)

    def showCards(self):
        return ''.join((str(x.number) + " ") for x in self.cards)

Player class:

class Player(object):
    def __init__(self, name, hand):
        self.name = name
        self.hand = hand

main function:

def main():
    deck = Deck()
    handA = [Card(6), Card(5), Card(3)]
    handB = [Card(10), Card(6), Card(5)]
    playerA = Player("A", handA)
    playerB = Player("B", handB)

    print("There are " + str(len(deck.cards)) + " cards in the deck.")
    print("The deck contains " + deck.showCards())

    for i in handA:
        deck.removeCard(i)
    print("Now the deck contains " + deck.showCards())

main()
like image 286
Ayushi Patel Avatar asked Jan 23 '26 09:01

Ayushi Patel


1 Answers

When you call list.remove, the function searches for the item in the list, and deletes it if found. When searching, it needs to perform a comparison, comparing the search item to every other list item.

You're passing an object to remove. A user defined object. They do not behave the same way as, say, integers would, when performing comparisons.

For example, object1 == object2, where object* are objects of the Card class, by default are compared against their unique id values. Meanwhile, you want a comparison to be performed against the card number, and removal done accordingly.

Implement an __eq__ method in your class (python-3.x) -

class Card(object):
    def __init__(self, number):
        self.number = number

    def __eq__(self, other):
        return self.number == other.number

Now,

len(deck.cards)
55

for i in handA:
    deck.removeCard(i)

len(deck.cards)
52

Works as expected. Note that in python-2.x, you'd implement __cmp__ instead.

like image 128
cs95 Avatar answered Jan 25 '26 22:01

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!