Skip to main content
Engineering LibreTexts

4.28: Step 3 – Placing the Icons on the Board

  • Page ID
  •     # Create the board data structure, with randomly placed icons.
        board = []
        for x in range(BOARDWIDTH):
            column = []
            for y in range(BOARDHEIGHT):
                del icons[0] # remove the icons as we assign them
        return board

    Now we need to create a list of lists data structure for the board. We can do this with nested for loops just like the generateRevealedBoxesData() function did. For each column on the board, we will create a list of randomly selected icons. As we add icons to the column, on line 6 [149] we will then delete them from the front of the icons list on line 7 [150]. This way, as the icons list gets shorter and shorter, icons[0] will have a different icon to add to the columns.

    To picture this better, type the following code into the interactive shell. Notice how the del statement changes the myList list.

    >>> myList = ['cat', 'dog', 'mouse', 'lizard']
    >>> del myList[0]
    >>> myList
    ['dog', 'mouse', 'lizard']
    >>> del myList[0]
    >>> myList
    ['mouse', 'lizard']
    >>> del myList[0]
    >>> myList
    >>> del myList[0]
    >>> myList

    Because we are deleting the item at the front of the list, the other items shift forward so that the next item in the list becomes the new "first" item. This is the same way line 7 [150] works.

    • Was this article helpful?