I'm practicing my progamming skills by solving problems from project euler at the moment, and now I've come across some (in my opinion) strange behavior on Python.
When I do:
list = [[1]]*20
I get a list of 20 lists containing element 1, as expected. However, when I would like to append a 2 to the third element from this list, I would do that as follows:
list[3].append(2)
This however changes ALL the elements in the list. Even when I take a detour, like:
l = list[3]
l.append(2)
list[3] = l
All my elements get changed. Can anyone please tell me how to do this and get an output like so:
[[1], [1], [1], [1, 2], [1] .... [1]]
Thanks in advance.