Difference between Mutable and Immutable Sequences in Python

What is a Sequence Type?

If we speak about sequences in Python there are two forms:

  • Mutable 
  • Immutable.

The main difference between these two is that a mutable sequence can be changed after its creation while an immutable sequence can’t. Most Common Mutable Sequences in python are List, Set and Dictionary.

Mutable Sequence in Python

Operations supported by Mutable Sequence Type

The main difference that makes mutable sequence type different from immutable sequence type is that it supports multiple operation that allows modification after it’s creation. The following operations are described for mutable sequences (where x is an arbitrary object):

Operations on Mutable SequenceChange after performing Operation
s[i] = xith  item of sequence “s” is replaced by “x”
s[i:j] = treplaces a slice of “s” from i index to j index with “t”

del d[i:j]

(same as s[i:j] = [])

deletes a slice of “s” from i index to j
s.append(x)adds x to sequence in last
s.extend(x)this is used only to extend list, it adds a list to existing list and also throws an exception if x is not a list object
s.count(x)returns number of occurrences of x in sequence
s.index(x)returns the first index where x is encountered
s.insert(i, x)inserts x at i index in  sequence
s.pop([i])removes object at i index from sequence it also returns the object which is removed
s.remove(x)removes x object from list
s.reverse()reverses the sequence
s.sort()sorts the sequence .
Types of Mutable Data Types in Python

Mutable Sequence vs Immutable Sequence

Mutable Sequence

  • Mutable Sequence can be modified after its creation by performing easy operations like append, delete, update.

  • Mutable Sequences are easy to update hence are less secure.

  • Mutable sequence are slower in terms of access as compared to immutable sequence.

  • Use of mutable sequence is recommended when we want to change data(content) or size dynamically.

Immutable Sequence

  •  Immutable sequence can’t be modified once created but it can be altered by making a copy with the updated data.

  • Immutable Sequence are a more secure option to store data as it can’t be changed after creation.

  • Immutable Sequences are quicker to access than mutable sequences.

  •  Use of an immutable sequence is recommended when we want more security for our data.

Operations supported by Immutable Sequence Type

The main difference that makes mutable sequence type different from immutable sequence type is that it supports multiple operation that allows modification after it’s creation. The following operations are described for mutable sequences (where x is an arbitrary object):

Operations on Immutable Sequence(tuple) Change after performing Operation
t[x] returns the element at index ‘x ‘of tuple ‘t’.
t[i:j] returns a slice of “t” from ith index to jth index
del t deletes whole tuple ‘t’
t.count(x) returns number of occuring of  ‘x’ in tuple ‘t’
t.index(x) returns the index at which object ‘x’ is situated in tuple ‘t’