After a little time banging my head against the wall, my example can be simply expressed as shown:
lists = [list()] * 5
print(f"Number of empty lists = {len([l for l in lists if len(l) == 0])}")
lists[0].append(0)
print(f"Number of empty lists = {len([l for l in lists if len(l) == 0])}")
lists = [list() for _ in range(5)]
print(f"Number of empty lists = {len([l for l in lists if len(l) == 0])}")
lists[0].append(0)
print(f"Number of empty lists = {len([l for l in lists if len(l) == 0])}")
This provides the following output:
Number of empty lists = 5
Number of empty lists = 0
Number of empty lists = 5
Number of empty lists = 4
The difference is the way in which the variable lists
is initialised. It seems that the first way is in fact just a lot of references to the same variable, whilst the latter is an actual list of independent variables. My question is why? What possible advantages could the first method provide? Why aren't these the same thing?
If I change one variable, say by appending to a list, they all change. There is a large chance for confusion and I cannot see why would these two methods not provide me with a list of copies.
If I wanted a list of the same thing over and over again, then why do I need a list at all - it seems like the code requires restructuring.
If I wanted to share properties of things but only sometimes, we have class attributes for that.
Thank you for any info in advance!