0

Why does only the first tuple change in example II whereas both lists get changed in example I? Please consider these two programs and their respective output (I and II).

I.

L1 = [1,2,3,4]
L2 = L1
L2.append(5)
print("L1: ", L1)
print("L2: ", L2)

Output:

L1: [1,2,3,4,5]
L2: [1,2,3,4,5]

II.

L1=(1,2,3,4)
L2=L1
L2 += (5,)
print("L1: ", L1)
print("L2: ", L2)

Output:

L1: (1,2,3,4)
L2: (1,2,3,4,5)
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
  • 1
    Please read https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples?rq=1 – R Simon Jul 19 '19 at 06:01
  • 1
    Also linked to [Why are list changes reflected in all copies?](https://stackoverflow.com/q/240178/9917694). The main idea is that lists are mutable and `L1 = L2` means both variables refer to the same object. Tuples are immutable so doing `+=` will create a new object. Although it appears to work in-place, it’s just assigning the result back to `L2` – N Chauhan Jul 19 '19 at 06:13
  • To illustrate what @NChauhan correctly said, try: `L2=L1; print(id(L1)); print(id(L2)); L2 += (5,); print(id(L2))`. That will show that L1 and L2 originally have the same ID - that is, they refer to the same object. After the `+=` step, L2 will have a new ID because it now points to a brand new object. – Kirk Strauser Feb 20 '20 at 03:03
  • Does this answer your question? [What's the difference between lists and tuples?](https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples) – Grismar Feb 20 '20 at 03:35
  • TL;DR: tuples are immutable, if you assign one to the other, you're getting a fresh copy - lists are mutable, if you assign one to the other, they're both the same list. – Grismar Feb 20 '20 at 03:36

3 Answers3

0

In List when you use this '=' assignment operator, it does not creates new reference, it refers same object.

In Tuple when you use this '=' assignment operator, it creates new reference.

Refer below code, check the output of ID.

L1 = [1,2,3,4]
L2 = L1
L2.append(5)
print("L1: ", L1)
print("L2: ", L2)
print("ID of L1: ", id(L1))
print("ID of L2: ", id(L2))
# Output =
# L1:  [1, 2, 3, 4, 5]
# L2:  [1, 2, 3, 4, 5]
# ID of L1:  2598160167616
# ID of L2:  2598160167616
# -------------------------------------------
L1=(1,2,3,4)
L2=L1
L2 += (5,)
print("L1: ", L1)
print("L2: ", L2)
print("ID of L1: ", id(L1))
print("ID of L2: ", id(L2))
# Output =
# L1:  (1, 2, 3, 4)
# L2:  (1, 2, 3, 4, 5)
# ID of L1:  2598161745696
# ID of L2:  2598161718864
# -------------------------------------------
Omkar
  • 3,253
  • 3
  • 20
  • 36
  • 1
    `In Tuple when you use this '=' assignment operator, it creates new object.` Wrong. *Everything* in python would create a new reference when written such as `b = a` (and thus, a new object is not created). It's only that tuples are immutable, so `L2 += (5,)` is where `L2` starts to point to a newly created tuple. – Paritosh Singh Feb 20 '20 at 04:25
0

In example 1, we have a list. List is mutable, which means once the lists are created they can be changed later. Hence the memory location at which they are stored remains the same. "L2=L1" statement is just providing reference of L1 to L2 and when changes are done in L2 they are reflected in L1 as well.

In example 2, we have tuples. Tuples on the other hand are immutable, which means they cannot be changed further. And that is the reason why L1 was not changing along with L2 because both of them are separate objects with separate memory references.

-1

Tuples are immutable, so only L2 is changing. As lists are mutable, both are changing then.

B--rian
  • 5,578
  • 10
  • 38
  • 89