Make a list dif


#1

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):
    board.append(5*["O"])
print board

output:
[['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):
         board.append(["O"*5])
    print board

output:
[['OOOOO'], ['OOOOO'], ['OOOOO'], ['OOOOO'], ['OOOOO']]


#2

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.

board.append(5*["O"])

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

board.append(["O"*5])

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.


#3

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):
board.append(i)
print ["O"] * 5

Can anybody tell me what i'm doing wrong?


#5

I am having the same problem.


#6

it is working
board=[]
for i in range(0,5):
board.append(["O"]*5)
print board


#7

From https://docs.python.org/3.1/tutorial/datastructures.html :

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

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])