if you want to clear out and modify the contents of a list in-place (as opposed to creating a new fresh list and assigning it back to the variable that holds your old list):
board[:] = startBoard
which, if nothing that needs to see the latest board contents points to the old list, is functionally equivalent to:
board = startBoard
you can get an idea of what it means to modify something in-place vs create a fresh copy/instance if you play around a little bit with assigning the same contents to multiple variables without making copies in the process.
Also, note that since startBoard
in your case is a constant, you want to make sure nothing ever modifies it, so the 2nd example is really a very bad idea. There's also however a 3rd:
board = startBoard[:]
which is actually by far the best of all three. What it does is make a copy of startBoard
and assign it to the variable board
without ever modifying the old list board
was pointing to. Also, because you're using a copy of startBoard
, you can be sure your start board is always the same in the future.
Note that these aspects arise only in programming languages or styles where mutable datatypes are used; in Functional Programming, almost nothing ever gets modified so you can completely become ignorant of worrying about causing accidental adverse side-effect by touching something someone else is relying on.
Furthermore: note that copying a list can be done in a multitude of ways in Python; from a high level point of view which I'm currently speaking out for there is little difference though, so copy.copy(startBoard)
is the same as [x for x in startBoard)
is the same as startBoard[:]
etc.
See also the PEP8 which provides you with useful coding conventions.