Search code examples
pythonlistnested-lists

List of Lists getting populated with null values


Learning python from a background in Java. Here is a class to represent a playing card deck. In this class is a method/function that will shuffle the deck by splitting the cards into x amount of stacks and then reversing order then merging back starting with y stack first.

The problem being the list of lists stakeholders is not getting populated with data, just populated with empty lists... have tested most other outputs/inputs, and seems to appear functioning as intended

appologies for the sloppy and or extensive overcoding

def mergeshuffle(self, x, y):
    if x > len(self.cards):
        print("Invalid input. Try again using an integer value 1-52")
        return
    elif x < 0:
        print("Invalid input. Try again using an posotive integer value 1-52")
        return
    
    stacks = int(len(self.cards)/x)
    cardsinstack = int(len(self.cards)/stacks)
    print(str(stacks) +" <- stacks cards in stack -> " + str(cardsinstack))
    counter = 0
    stackholder = []   
       
    for i in range(stacks): 
        thisstack = []               
        for x in range(cardsinstack):
           thisstack.append(self.cards[counter])
           #print (" Added " + str(thisstack[x]))  
           #print(str(counter))
           counter=counter+1
        thisstack.reverse()
        stackholder.append(thisstack)
        thisstack.clear()
    print(str(stackholder))
    #print((str(stackholder)).center(10, '&') )

The output (relevant output)

The deck added 2 of Spades
The deck added 3 of Spades
The deck added 4 of Spades
The deck added 5 of Spades
The deck added 6 of Spades
The deck added 7 of Spades
The deck added 8 of Spades
The deck added 9 of Spades
The deck added 10 of Spades
The deck added Jack of Spades
The deck added Queen of Spades
The deck added King of Spades
The deck added Ace of Spades
The deck added 2 of Clubs
The deck added 3 of Clubs
The deck added 4 of Clubs
The deck added 5 of Clubs
The deck added 6 of Clubs
The deck added 7 of Clubs
The deck added 8 of Clubs
The deck added 9 of Clubs
The deck added 10 of Clubs
The deck added Jack of Clubs
The deck added Queen of Clubs
The deck added King of Clubs
The deck added Ace of Clubs
The deck added 2 of Diamonds
The deck added 3 of Diamonds
The deck added 4 of Diamonds
The deck added 5 of Diamonds
The deck added 6 of Diamonds
The deck added 7 of Diamonds
The deck added 8 of Diamonds
The deck added 9 of Diamonds
The deck added 10 of Diamonds
The deck added Jack of Diamonds
The deck added Queen of Diamonds
The deck added King of Diamonds
The deck added Ace of Diamonds
The deck added 2 of Hearts
The deck added 3 of Hearts
The deck added 4 of Hearts
The deck added 5 of Hearts
The deck added 6 of Hearts
The deck added 7 of Hearts
The deck added 8 of Hearts
The deck added 9 of Hearts
The deck added 10 of Hearts
The deck added Jack of Hearts
The deck added Queen of Hearts
The deck added King of Hearts
The deck added Ace of Hearts
52 playing cards total.
13 <- stacks cards in stack -> 4
[[], [], [], [], [], [], [], [], [], [], [], [], []]

hoping the list of lists would be populated with lists of len() = 4 containing the above cards in groups of 4 sequentially as printed.

I have tried testing every other variable in the program to see how it is operating and it seems as far as I have conducted research to be functioning properly as some of the noted-out print statements may indicate.


Solution

  • thisstack.clear()
    

    Lists, in Python, are mutable. This means that you can append a list to a list, and then clear the inner list. By clearing this list, even after the list is appended to another list, you're clearing the list within that list.

    Instead of clearing the list, you should create a new list. You can do that like so:

    thisstack = []
    

    However, you're already doing that at the top of your loop, so you can just delete the line thisstack.clear().