At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
It is going to be along read and at points you may think it is becoming irrelevant, but I have tried to come around the answer to your question. So, brace yourself!! :) I haven't worked a lot in python. However in computer science, generally, there is this idea of sameness and equality. In python operators, procedures and methods are technical terms and they all are different from each other. However there are other programming languages in which all these are technically the same (if you have spare time, look at Scheme). There is a very important concept of an environment (there could be other subtitutes of the term). But the general idea is that an expression x+ y makes no sense until an up till there isn't an environment associated with it where we may have the information for x and y. This expression can lead to errors or different answer depending on what x and y are bound to as variables in the environment in which we are evaluated this expression. Now that we know a little about environments, and we already know about procedures, we can discuss the issue of sameness and equality. First of all, if variable a is bound to list ['spam', 'eggs', 100, 1234], >>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234] Now, if we do a couple of more lines >>> b = ['spam', 'eggs', 100, 1234] >>> c = a Now you should notice that a and b are equal but NOT same. But a and c are same and equal. Because a and b both have been exclusively assigned there values. It happened that the values they were assigned were copies of each other but doing c = a means that c points to the value a is pointing to. Proceeding further, all procedures/methods are either predefined, or we ourselves define them. Procedures and methods can and cannot need data to complete the task they are supposed to do: arguments, objects etc. We need a crude example otherwise the chances of you getting confused will get higher. Python supports lambdas (hope you know a little about them). A lambda can be defined with two arguments lets say x and y. Whenever we use the lambda, we need to give an x and y. Consider a and b to be what we defined above. We can pass this a and b as arguments to this lambda. Now you should notice that no matter what we may have done with x and y in the body of lambda (when we defined it), it will not change our a and b. Now comes our idea of environments in use. We have a global environment in which a and b are bind to something. When we use the lambda, a local environment is created in which the variables x and y are bound to the VALUES of a and b. (NOTICE: x and y are equal to a and b but x and y are NOT the same as a and b, respectively)...But then there are special forms/operators etc. They don't create copies of the variables but have access to the SAME original object. So when we make changes with that object, it changes the original object. So extend and append just like you mentioned, under the hood, create their own personal copies of data. Changing these new copies doesn't affect original objects. Whereas the slicing operator has access to THAT SAME ORIGINAL list (the original object)..any changes you make there will reflect automatically. Have any further questions, feel free to ask!
its simple, when you are copying using [ : ] or via copy.copy (from the copy module) you are doing what is called as 'shallow copy'. Here a new object is created but the elements of the objects are pointing towards the original objects. You can know weather they are pointing towards same object by using the function id(x), where x is the desired object. you can append or extend new elements to either lists without affecting other. But when you change any object that is being commonly pointed by both the list ( or in simple words, any object that is present at the time of copying), it will be reflected in both the lists. A better way to copy to avoid all this is to use copy.deepcopy x = [1,2,3,[ 'a','b'] ] y = x[ : ] is same as y = copy.copy(x) , changes of objects that are already present in x or y will be reflected to both lists. new additions(by append or extend) to x or y will not be reflected to the other one. id(x) == id(y) will be true y = copy.deepcopy(x) compelete copy ,new references. x and y are completely different here id(x) == id(y) will be false
So, with shallow-copy, the reference to each list in the nested list is being copied as if each being treated as an element in itself?? @raghue.... and thanks.