Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Void Group Title

I am using python 2.7. Suppose i create a list within a list. If i copy the whole thing using slicing operator (like list2=some_list[:] ) and make changes to the newly created list, the changes are reflected back to the original list. (But extend and append functions work perfectly! ) I can't seem to figure out why.. help!

  • one year ago
  • one year ago

  • This Question is Open
  1. azka.niazi Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

    • one year ago
  2. raghue Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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[3]) == id(y[3]) will be true y = copy.deepcopy(x) compelete copy ,new references. x and y are completely different here id(x[3]) == id(y[3]) will be false

    • one year ago
  3. Void Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.