0

Code: (Python3)

l = [[[False,False]]*3]*3
l[0][0][0] = True
print(l)

Output:

[[[True, False], [True, False], [True, False]], [[True, False], [True,          False], [True, False]], [[True, False], [True, False], [True, False]]]

I just cant figure out why the second line of code changes every single first element instead of just l[0][0][0]. i feel like im missing something really obvious. if anybody could shed some light that would be great. Thanks :)

karthikr
  • 97,368
  • 26
  • 197
  • 188
b9703
  • 107
  • 2
  • 11
  • By doing `*3` you are creating a reference of the same object 3 times. Hence the result. – karthikr Jul 21 '15 at 23:51
  • They contain the same references because they are the same literal. – Malik Brahimi Jul 21 '15 at 23:53
  • ah that makes sense now. is there an easy way to get the result i want besides just using l.extend([]) in a for loop. the actual code im writing has an input N instead of the 3s – b9703 Jul 21 '15 at 23:56

0 Answers0