5

I am new to Python. Here is a question I have about lists: It is said that lists are mutable and tuples are immutable. But when I write the following:

L1 = [1, 2, 3]
L2 = (L1, L1)
L1[1] = 5
print L2

the result is

([1, 5, 3], [1, 5, 3])

instead of

([1, 2, 3], [1, 2, 3])

But L2 is a tuple and tuples are immutable. Why is it that when I change the value of L1, the value of L2 is also changed?

1
  • Seems like you create a view and not a deep copy?
    – Jakob S.
    Sep 1, 2012 at 20:31

7 Answers 7

7

From the Python documentation (http://docs.python.org/reference/datamodel.html), note:

The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.

4

The tuple is immutable, but the list inside the tuple is mutable. You changed L1 (the list), not the tuple. The tuple contains two copies of L1, so they both show the change, since they are actually the same list.

If an object is "immutable", that doesn't automatically mean everything it touches is also immutable. You can put mutable objects inside immutable objects, and that won't stop you from continuing to mutate the mutable objects.

4

The tuple didn't get modified, it still contains the same duplicate references to list you gave it.

You modified a list (L1), not the tuple (or more precisely, not the reference to the list in the tuple).

For instance you would not have been able to do

  L2[1] = 5

because tuples are immutable as you correctly state.

So the tuple wasn't changed, but the list that the tuple contained a reference to was modified (since both entries were references to the same list, both values in the output changed to 5). No value in the tuple was changed.

It may help if you think of reference as a "pointer" in this context.

EDIT (based on question by OP in comments below):

About references, lists and copies, maybe these examples will be helpful:

L=range(5)
s = (L, L[:]) # a reference to the original list and a copy

s
([0, 1, 2, 3, 4], [0, 1, 2, 3, 4])

then changing L[2]

L[2] = 'a'

gives:

s
([0, 1, 'a', 3, 4], [0, 1, 2, 3, 4])  # copy is not changed

Notice that the "2nd" list didn't change, since it contains a copy.

Now,

L=range(5)

we are creating two copies of the list and giving the references to the tuple

s = (L[:], L[:])

now 
L[2] = 'a'

doesn't affect anything but the original list L

s
 ([0, 1, 2, 3, 4], [0, 1, 2, 3, 4])

Hope this is helpful.

3
  • Thanks. That's very helpful. When I mention a list in defining a variable, is there any general rule telling me when the list is merely contained as a reference, and when the list is contained as a copy? For example if I define the variable A as L1[1], L1 is contained as a copy, but as you said, when I define L2 as [L1, L1], L1 is contained as a reference.
    – Rachaely
    Sep 1, 2012 at 21:35
  • @user1641021 If you use the slice notation (e.g., L1[:]), you are making a copy of the list. I'll ad a bit more on this to my answer, perhaps it will be helpful.
    – Levon
    Sep 1, 2012 at 21:39
  • @user1641021 I updated my answer to address some of your questions, I hope it helps.
    – Levon
    Sep 1, 2012 at 21:46
2

You're right that tuples are immutable: L2 is an immutable tuple of two references to L1 (not, as it might first appear, a tuple of two lists), and L1 is not immutable. When you alter L1, you aren't altering L2, just the objects that L2 references.

2

Use deepcopy instead of = :

from copy import deepcopy
L2 = deepcopy(L1)

1

The tuple contains two references, each to the same list (not copies of the list, as you might have expected). Hence, changes in the list will still show up in the tuple (since the tuple contains only the references), but the tuple itself is not altered. Therefore, it's immutability is not violated.

4
  • If I write L1 = [1, 2, 3] A = L1[1] L1[1] = 5 Print A
    – Rachaely
    Sep 1, 2012 at 21:09
  • @user1641021: in that snippet, first you make a list and name it L1. Then you bind the name A to the object referred to by L1[1], which is the integer 2. Then you change the second element of L1 to be 5. Then you print A, which is still 2; you "pointed" A at the integer 2, not at "the second element of L1".
    – DSM
    Sep 1, 2012 at 21:12
  • But if I write L1 = [1, 2, 3] A = L1[1] L1[1] = 5 Print A The result will be 2 instead of 5. Is that because 'L1[1]'contains a copy of L1, not a reference of L1, so that even L1 changed its value, L1[1] wouldn't?
    – Rachaely
    Sep 1, 2012 at 21:15
  • thanks. sorry I just realized you answered my question. I thought my first response didn't come through.
    – Rachaely
    Sep 1, 2012 at 21:17
0

Tuples being immutable means only one thing -- once you construct a tuple, it's impossible to modify it. Lists, on the other hand, can be added elements to, removed elements from. But, both tuples and lists are concerned with the elements they contain, but not with what those elements are.

In Python, and this has nothing to do with tuples or lists, when you add a simple value, like an int, it gets represented as is, but any complex value like a list, a tuple, or any other class-type object is always stored as reference.

If you were to convert your tuple to a set(), you'd get an error message that might surprise you, but given the above it should make sense:

>>> L=range(5)
>>> s = (L, L[:]) # a reference to the original list and a copy
>>> set(1, 2, s)
>>> set((1, 2, s))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

As values of a set must never change once they are added to the set, any mutable value contained inside the immutable tuple s raises TypeError.

1
  • This makes it seem like ints aren't "stored by reference" in the same way that "complex value"s are, and are instead "represented as is". But an int is an object in exactly the same way a tuple is.
    – DSM
    Sep 2, 2012 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.