Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Has anyone done quiz 1? I'm confused by the answer to 1.5: "Let L be a list, each element of which is a list of ints. In Python, the assignment statement L[0][0] = 3 mutates the list L." Supposedly the answer is: "FALSE, it mutates the list at L[0]." Is there a word missing or something? If L[0] is mutated, isn't L as a whole considered mutated??

OpenStudy (rsmith6559):

Not really. L[0] is a list in it's own right. L only contains a bunch of lists, one of which was mutated, but nothing directly changed for L. The mutation was to one of L[0]'s integers.

OpenStudy (anonymous):

That seems so debatable. Again, since L contains the mutated list... Do you have a source that contains that explanation? Does it have something to do with the way the list is stored in memory?

OpenStudy (anonymous):

I don't remember which video it is unfortunately, but in one of the early ones the Prof spends some time on this. Basically think of a list as a named collection. A list contained in another list is still just a named collection, not necessarily the contents of said collection. Here's an example, say you have a list of your possessions broken up into categories. One category could be your clothes. If you get a new shirt, the contents of the collection have changed, but the high level list hasn't actually changed, because you've already listed your clothes as one whole collection.

OpenStudy (anonymous):

Ah ok, I missed that somehow. Thanks!

OpenStudy (anonymous):

As far as I understand the implementation, the list object consists of a bunch of pointers pointing to other elements in memory. So as long as L's pointer at 0 points to the same list object, L is considered unchanged. Although, to be fair, I'm still having trouble fully understanding the concept myself.

OpenStudy (anonymous):

It sounds like you've just about got it, Ante. Just keep in mind that while your parent list can be a list of different types of data, each element can only contain one thing. You cannot simultaneously store a list AND it's contents in a single element. so the sub list as far python is concerned is nothing more than another named collection (whose contents it doesn't care about)

OpenStudy (anonymous):

Thanks for responding to that. Lists and tuples still confuse me. Is is correct to say that the main difference between them is that tuples take up an actual section in memory where all their elements are stored as bit strings and list objects themselves only contain references to other memory sections?

OpenStudy (rsmith6559):

They're probably both implemented with memory pointers. From a Python programmer's perspective, the main difference between lists and tuples is that lists are mutable ( can be changed ), and tuples are immutable ( can't be changed ). If you "change" a tuple, you actually create a new tuple. A list is changed "in place".

OpenStudy (anonymous):

Thanks for the answer, it makes sense.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!