Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the content of a tuple changes when I append to a list inside of it but does not change when I update a variable?

myVar = ["jhhj", "hgc"]
myTuple = ([1,2,3], [4,5,6], myVar)
myVar.append('lololol')
print myTuple

Why and how can this tuple be modified by appending after construction?

myVar = "lol"
myTuple = ([1,2,3], [4,5,6], myVar)
myVar = "lolol"
print myTuple

Why is this going to print out ([1,2,3], [4,5,6], "lol") instead of ([1,2,3], [4,5,6], "lolol")?

like image 513
Web Master Avatar asked Jun 14 '12 02:06

Web Master


People also ask

Does append work with tuples?

You can't add elements to a tuple because of their immutable property. There's no append() or extend() method for tuples, You can't remove elements from a tuple, also because of their immutability.

Can a tuples contents be changed?

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Are tuples mutable inside list?

Mutable List vs Immutable Tuples. List has mutable nature i.e., list can be changed or modified after its creation according to needs whereas tuple has immutable nature i.e., tuple can't be changed or modified after its creation.

Can we update tuple in Python?

Practical Data Science using Python Python tuple is an immutable object. Hence any operation that tries to update it is not allowed.


2 Answers

Well, let me try to explain with some images.

In Python, everything is an object. Those objects are referenced by variables. Some kinds of objects, such as lists and tuples, just store references to other objects.

That said, when you execute

myVar = ["jhhj", "hgc"]
myTuple = ([1,2,3], [4,5,6], myVar)

You get more or less this scenario:

A tuple pointing to three lists; a list pointing to two strings

Each object is represented by a box/rectangle. We have two string objects, "jhhj" and "hgc". Also, we have a list object, pointed by the variable myVar; this list object points to both string objects. Also, we have a tuple object, referenced by myTuple; this tuple object points two other lists and the list referenced by myVar.

When you execute

myVar.append('lololol')

what does happen? Well, the list object (that is incidentally pointed by myVar) starts to referenced one more value, the string object "lololol":

A tuple pointing to three lists; a list pointing to two strings; a new string is added to the list

Note that myVar still references the list object. What happened is that the list object changed. You can look at this list object both from myVar or from the tuple, you will see the same object with the same change.

OTOH, when you execute

myVar = "lol"
myTuple = ([1,2,3], [4,5,6], myVar)

You get something like this:

A tuple pointing to two lists and a string, also pointed by a variable

Now myVar points to the string object "lol", as well the tuple references it at its third position. Now, if you execute

myVar = "lolol"

you are just making myVar to point to another object. The tuple object still points to "lol" as before:

A tuple pointing to two lists and a string, also pointed by a variable. The variable now references another string.

So, if you attribute a new value to a variable, it will just change the value pointed by this variable. The previous value referenced by the variable will still exist* and any other variable or object pointing to it will stay pointing to it. Just the attributed variable will change.

PS: Also, I answered a vaguely related question some time ago. You may find the answer helpful.

* Except if it is collected by the garbage collector, but this is another long history.

like image 190
brandizzi Avatar answered Sep 22 '22 04:09

brandizzi


All things in python are objects.

So when you do your original assignment

myVar = "lol"

you are giving myVar a reference to the object "lol"

You then create a tuple. This tuple in the third slot has a reference to "lol"

Then you create a new object "lolol" and give myVar a reference to it. The tuple retains its original reference to "lol"

like image 43
Anthony Sottile Avatar answered Sep 20 '22 04:09

Anthony Sottile