Battleship question (a dumb one)


board = []
for i in range(5):
    board.append(5 * ["O"])

def print_board(board):
    for x in board:
        print x

My code is all fine so far. What I can't get my head around is how board.append(5 * ["O"]) makes 5 rows of lists in one empty list.

I thought that board.append would then have to be multiplied a further 5 times to make a grid. Please explain how I'm wrong as I'm panicking and thinking that I will just never get Python!

edit: is it kind of like board.append(5 * x) * 5?


it doesn't, board.append(5 * ["O"]) creates the column. By doing this 5 times (loop) you will generate 5 rows


so range(5) is how it is looped 5 times then?


that is what the loop does?

for i in range(5):

will loop 5 times, as you can see when running this:

for i in range(5):
    print i
    board.append(5 * ["O"])

if in python we want the loop to run an x amount of times, we can use range (which produces a list, which we then loop over)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.