Python List vs tuple mutability

Python List vs tuple mutability


Python List vs Tuple is always a confusing question if one does not understand the basics well. Python List and Tuples are more or less similar to each other until and unless they are not modified. The big difference between these two is that list is mutable and tuple is immutable.

An object being mutable means that it can be changed in place and hence there is no new object instantiated in the process, while in the case of immutability an object cannot be changed in place.

Because of its immutable nature tuple is considered a good container if there is no need for modification, like serving as a multidimensional key in a dictionary. But if we want we can still get results similar to list using a tuple.

Python List vs Tuple

Let us briefly see the comparison between list and tuple then we will solely focus on the mutability difference between these two.

List
  • List is mutable.
  • list consumes more memory.
  • list have built in functions for modification operation.
  • insertion and deletion is fast.
  • iteration process is slow.
Tuple
  • Tuple is immutable.
  • Tuple consumes less memory as compared to list.
  • Tuple lacks built in fuction for different modification operations.
  • insertion and deletion is slow and cumbersome.
  • iteration process is faster than list.

Let’s now focus on the mutability-based difference between list and tuple.

List modification

There are several built in functions available for list manipulation like append(), extend(), insert(), pop(), remove().
Let us try the different operations on the same list and track the uniqueness of the list using id().

List modification code

# 1. Initialising the list 
mylist = [1,2,3]
print('hex(id(mylist)),mylist)

# 2.Updating value at an index
mylist[0] = 0
print(hex(id(mylist)),mylist)

# 3. Appending an element using '+'
mylist += [4]
print(hex(id(mylist)),mylist)

# 4. Appending an element
mylist.append(5)
print(hex(id(mylist)),mylist)

# 5. Extending the list
mylist.extend([6,7])
print(hex(id(mylist)),mylist)

# 6. Popping an element
mylist.pop()
print(hex(id(mylist)),mylist)

#7. Removing an element
mylist.remove(3)
print(hex(id(mylist)),mylist)

Output

0x7f8746dc8688 [1, 2, 3]
0x7f8746dc8688 [0, 2, 3] 
0x7f8746dc8688 [0, 2, 3, 4] 
0x7f8746dc8688 [0, 2, 3, 4, 5] 
0x7f8746dc8688 [0, 2, 3, 4, 5, 6, 7] 
0x7f8746dc8688 [0, 2, 3, 4, 5, 6] 
0x7f8746dc8688 [0, 2, 4, 5, 6] 

list mutation using visual representation

Python list and tuple mutability can be easily understood, if we go one step ahead and add some visual explanation. Below images illustrates that after applying different modification function on the list the object id is the same showing that modification is in place.
list1 1

list2 1

list3 1

list4 1

list5 1

list6 2

list7 1

Here, the uniqueness of mylist is consistent throughout, ie. mylist keeps referring to the same list object even after the different operations performed on it, and hence, those were in place operations making the list mutable in nature.

Tuple modification:

There is no build-in modification function for tuples and we cannot modify it in place. So, technically we cannot modify a tuple but yes, we can get the operation as of list with the help of concatenation and slicing, ignoring the performance issue.

Operations on tuple similar to list but using concatenation and slicing

# 1. Initialisation of tuple
mytuple = (1,2,3)
print(hex(id(mytuple)),mytuple)

# 2. Appending the tuple
mytuple += (4,)
print(hex(id(mytuple)),mytuple)

# 3. Extending the tuple
mytuple += (6,7)
print(hex(id(mytuple)),mytuple)

# 4. Adding any number at certain position
pos=2
num=9
mytuple = mytuple[:pos]+(num,)+mytuple[pos+1:]
print(hex(id(mytuple)),mytuple)

# 5. Popping an element
mytuple = mytuple[:len(mytuple)-1]
print(hex(id(mytuple)),mytuple)

Output

0x7fed22d84510 (1, 2, 3) 
0x7fed22d61db8 (1, 2, 3, 4) 
0x7fed22dd7708 (1, 2, 3, 4, 6, 7) 
0x7fed22dd7b28 (1, 2, 9, 4, 6, 7) 
0x7fed22e98780 (1, 2, 9, 4, 6)

Visual representation of a sequence of operations on tuples

tuple1
In the above figure, every object has got a number in front of itself according to the sequence number given to the statement (in respective comments in the code) responsible for its instantiation.
So, this is clear that tuple is not meant for modification and if done, every time a new object gets created and performance is also an issue if the tuple is large in size while list is good for modification and efficiency.