Make a List


#1


https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/3?curriculum_id=4f89dab3d788890003000096#

Why can't I create a list outside my for loop and call it later in my for loop. The code works if you type '["O"] * 5' in the .append() function


I expected the function to work with Os typed in the .append() funciton


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


#2

The problem is that you are trying to append the same reference 5 times. In stead of adding a row five times. If you append a row separately it works:

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

#3

I understood what you said. But I think I am doing the exact same thing by having the list assigned to a variable first (The one I called 'Os') and then calling it in the function .append().


#4

The problem that you are to actually adding the row itself, but you keep adding a reference to the same variable Os. If you run your code you actually get a Error Message stating this right?


#5

Yes I got that error message.

But I still think putting it out as board.append(Os) is same as board.append(["O"] * 5).
because I assigned Os = ["O"] * 5.

How I understand this is as follows:

I first assigned Os = ["O"] * 5. This means, Os = ["O", "O", "O", "O", "O"]

After this I created a loop that makes a list containing 5 lists in it as 5 elements. I have added board.append(Os). When the compiler reaches the 'Os' part in board.append(Os), it expands to be board.append(["O"] * 5) because I assigned it earlier as Os = ["O"] * 5 and then prints it as follows:

For i = 0:

[["O", "O", "O", "O", "O"]]

For i = 1:

[["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"]]

For i = 2:

[["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"]]

For i = 3:

[["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"]]

For i = 4:

[["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"]]

If my logic is wrong. Can you explain in detail like how I did so that I can understand better? Thanks. :slight_smile:


#6

Hey I just checked it in a different Python editor and what you are saying seems to be true. It works just as you thought. I do not understand why it is giving the error message here. Maybe codecademy just counts it als false because it is not the way it was instructed. Sorry, for confusing you :stuck_out_tongue:


#7

No problem mate. Well, at least it is clear now. Thanks for your time! :slight_smile:


#8

there is a good reason codecademy doesn't approve the code, yes it looks fine, but there is a problem. I am jumping a few lessons ahead, but lets say the user made an incorrect guess, we want to mark this on the board using an "X":

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

multiply X will show, because Os all refers to the same list. Is undesired behavior, not something you want to have. Its should only mark a single spot, this is why you need to do the multiplication of the list inside the for loop


#10

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