Keeping an object static in python [duplicate]

  python

I’m sure this is a basic question and is in the help file somewhere, the problem is I’m not a programmer and not sure the right terminology to use to search for it. Anyway, I’m having trouble with one object updating dynamically instead of importing static values (again, probably the wrong technical terms but the closest I can get). For instance:

L=[]
O=[]
i=0
while i<3:
  O.append(i)
  L.append(O)
  i+=1
print(L)

I want it to return [[0],[0,1],[0,1,2]] but instead I get [[0,1,2],[0,1,2],[0,1,2]]. Basically the list L seems to be treating O as a variable and simply returning the ending value [0,1,2] three times instead of listing the three different values of O at the time I appended them to L. How do I convince it to do the latter?

Source: Python Questions

LEAVE A COMMENT