0

I create the following list of list containing False:

array=[[False] * 3] * 5

How can I change only value value?

array[1][0]='change only this'

Currently the result is

[['change only this', False, False],
 ['change only this', False, False],
 ['change only this', False, False],
 ['change only this', False, False],
 ['change only this', False, False]]

which is not desired. I understand each column is one single object [False] * 3]. How can I decouple them from each other. Do I need to make them immutable somehow?

Nickpick
  • 5,569
  • 14
  • 57
  • 107

1 Answers1

2

You could write

array = [[False] * 3 for _ in range(5)]

This creates five separate three-element lists and stores the references in array.

NPE
  • 464,258
  • 100
  • 912
  • 987