0

I have declared a (5x6) list called path in Python as:

path = [[' ']*6]*5

Now, when I change the value of any one element as:

path[4][5] = '*'

or

path[0][5] = '*'

I get the output as:

[' ', ' ', ' ', ' ', ' ', '*']
[' ', ' ', ' ', ' ', ' ', '*']
[' ', ' ', ' ', ' ', ' ', '*']
[' ', ' ', ' ', ' ', ' ', '*']
[' ', ' ', ' ', ' ', ' ', '*']

That is, all the elements of the specified column get changed to '*'. Why does this happen? My guess is that maybe this has something to do with the way this short-hand notation is implemented in Python. Maybe the interpreter takes the first row and copies it down for all columns.

  • You are exactly right, you have 5 duplicates of the initial list and so changes to any item in any will propogate throughout the structure – PyNEwbie Mar 21 '16 at 16:44
  • I am confused though as to why your output is full of blanks and not 0s – PyNEwbie Mar 21 '16 at 16:45
  • I'm sorry, I entered a zero in the list my mistake. It was supposed to be a space. –  Mar 21 '16 at 19:16

0 Answers0