Make a list dif


I have generated the list by using board.append(5*["O"]. I am not clear about the difference between board.append(5*["O"] and board.append(["O"*5]). I request friends to help me in this. I feel there is no difference between the two ouputs. Thanks

board =[]
for p in range(5):
print board

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

    board =[]
    for i in range(0,5):
    print board

[['OOOOO'], ['OOOOO'], ['OOOOO'], ['OOOOO'], ['OOOOO']]


You pretty much just gave yourself the answer, at least visually. You're making a battleship game where each "O" is supposed to represent a spot on the board.


^ This allows for separate spaces on a 5 by 5 board for you to choose from. Whereas:


would make a small grid where each "O" would be, if I'm not mistaken, impossible to select individually. If you were doing this outside of the course, you would need to add more [OOOOO] just to make a 5 by 5 grid.

So for the first output, you get 5 separate O's that you can choose from. In the second output, you get 5 groups of 5 O's to choose from. Each [OOOOO] would count as one square.


I keep getting an error message also, but the output looks fine. The error message says, "It looks like the rows are not represented as lists."

Here's my code:

board = []
for i in range(0,5):
print ["O"] * 5

Can anybody tell me what i'm doing wrong?


I am having the same problem.


it is working
for i in range(0,5):
print board


From :

Add an item to the end of the list; equivalent to a[len(a):] = [x].

Evaluate the argument you are passing through board.append by itself to see the difference better:

>>> ( 5 * ["O"])
['O', 'O', 'O', 'O', 'O'] \\ a list with 5 elements that contain 'O'

>>> ([5*"O"])
['OOOOO'] \\ a list with 1 element that contains 'OOOOO'

Remember that the square brackets [] represent a list element and are not just arbitrary delimiters, thus:

(A * [ B ] ) != ([A*B])