Omar
Omar

Reputation: 1

Python: is there a better way to append multiple different objects to a list

I am trying to create a monopoly deal game on python and I am having a bit trouble of initializing a list of objects. Each card class has a kind which is the type of card and a value. I want to create a list of different cards that have different quantities

My code so far looks like this:

class Card:
    def __init__(self, value,kind):
        self.value = value
        self.kind = kind

    def __str__(self):
        return "{} : {}".format(self.value,self.kind)

    def __repr__(self):
        return str(self)

    def show(self):
        print(self)
class Deck:
    def __init__(self):
        self.cards = []
        self.build()

    def build(self):
        #money cards
        self.cards.extend([Card(1,"money")] * 6)
        self.cards.extend([Card(2, "money")] * 5)
        self.cards.extend([Card(3, "money")] * 3)
        self.cards.extend([Card(4,"money")] * 3)
        self.cards.extend([Card(5,"money")] *2)
        self.cards.extend([Card(10,"money")])
        #action cards
        self.cards.extend([Card("dealbreaker", "action")] * 2)
        self.cards.extend([Card("justsayno", "action")] * 3)
        self.cards.extend([Card("passgo", "action")] * 10)
        self.cards.extend([Card("forceddeal", "action")] * 3)
        self.cards.extend([Card("slydeal", "action")] * 3)
        self.cards.extend([Card("debtcollector", "action")] * 3)
        self.cards.extend([Card("birthday", "action")] * 3)
        self.cards.extend([Card("doublerent", "action")] * 2)
        self.cards.extend([Card("houses", "action")] * 3)
        self.cards.extend([Card("hotels", "action")] * 2)
        #property cards
        self.cards.extend([Card("blue", "property")] * 2)
        self.cards.extend([Card("brown", "property")] * 2)
        self.cards.extend([Card("utility", "property")] * 2)
        self.cards.extend([Card("green", "property")] * 3)
        self.cards.extend([Card("yellow", "property")] * 3)
        self.cards.extend([Card("red", "property")] * 3)
        self.cards.extend([Card("orange", "property")] * 3)
        self.cards.extend([Card("pink", "property")] * 3)
        self.cards.extend([Card("lightblue", "property")] * 3)
        self.cards.extend([Card("railroad", "property")] * 4)

Now I want to know if there is a better way to create my list of cards without this much repetition. I couldn't find a way to loop and append since every card must have its own type and value, and there are different quantities of each card.

Please let me know if I am missing something. Is there a way to do this with list comprehension? Any help is appreciated, thanks in advance!

Upvotes: 0

Views: 122

Answers (2)

Rafael Setton
Rafael Setton

Reputation: 397

The only way I can think of that would reduce the amount of code would be to sum all the lists beforehand and then appending to the main list.

e.g.turn this:

        self.cards.extend([Card("blue", "property")] * 2)
        self.cards.extend([Card("brown", "property")] * 2)
        self.cards.extend([Card("utility", "property")] * 2)
        self.cards.extend([Card("green", "property")] * 3)
        self.cards.extend([Card("yellow", "property")] * 3)
        self.cards.extend([Card("red", "property")] * 3)
        self.cards.extend([Card("orange", "property")] * 3)
        self.cards.extend([Card("pink", "property")] * 3)
        self.cards.extend([Card("lightblue", "property")] * 3)
        self.cards.extend([Card("railroad", "property")] * 4)

into this:

    self.cards.extend([Card("blue", "property")] * 2 + 
        [Card("brown", "property")] * 2 + 
        [Card("utility", "property")] * 2 + 
        [Card("green", "property")] * 3 + 
        [Card("yellow", "property")] * 3 + 
        [Card("red", "property")] * 3 + 
        [Card("orange", "property")] * 3 + 
        [Card("pink", "property")] * 3 + 
        [Card("lightblue", "property")] * 3 + 
        [Card("railroad", "property")] * 4)

Upvotes: 0

iBug
iBug

Reputation: 37217

Store your card configuration in a list-of-lists like [[1, "money", 6], ...] so you can loop over it:

CARD_CONFIG = [
    [1, "money", 6],
    [2, "money", 5],
    [3, "money", 3],
    [4, "money", 3],
    [5, "money", 2],
    [10, "money", 1],
    ...
]

for value, kind, qty in CARD_CONFIG:
    self.cards.extend([Card(value, kind)] * qty)

Upvotes: 2

Related Questions