Javatpoint Logo
Javatpoint Logo

Which Python data structure is immutable?

Before we go into the category of data structure which is mutable and which is an immutable data structure, let us first discuss in brief the concept of mutability and immutability.

Mutable data structures

Mutable data types are the data types that can be further modified or changed once the program is executed. Suppose we have assigned a value to the variable as 10, then click on the execution button. If we want to update the value of that variable with 12a mutable, then we can change the value of that variable. There is no limit to the number of times the variable is updating in the program until we apply another kind of restrictions over it. Data can be changed further in these mutable data structures, overwritten, replacing the previous data and updating the data structure with new content. Mutable data types are the data items that can be adaptable, changeable and altered by removing the element, adding the data element, or if we want to replace any data item.

Immutable data structures:

Immutable data types are the data types that cannot be further changed or modified once executed in the program. Suppose we have assigned a value to the variable as 10, click on the execution button. After that, if we want to update the value of that immutable variable with 12, we cannot change the value of that variable until we create that variable again. There is no solution to do manipulations in that variable.

Certain data types are immutable, which cannot be changed further after executing a program like an int, float, bool, string, Unicode, tuple.

Immutable data structures are very handy when you want to prevent multiple people from modifying a piece of data in parallel programming at the same time. Mutable data structures ( e.g., Array ) can be changed at any time, while immutable data structures cannot be.

Python's default data structures are mutable, which is a sensible design decision in terms of least surprise, but understanding the initialization and state of a mutable structure is hard, especially in a language like a python that freely allows reassignment executes like a script rather than a compiled language.

Immutable data structures are the answer. Python supports some immutability natively through named tuples. However, these are better replacements for objects than data structures like List and Dict.

Data structures in python which is immutable

Tuples in python

Tuples are the collection of python objects separated by commas. Tuples are similar to the list in terms of indexing, repetition, and nested objects. But here, the list is mutable and can be changed further according to the need of the programmer.

Python program to change the value inside the tuple

Output

(0, 1, 2, 3, 4, 5)
---------------------------------------------------------------------------
TypeError                                 Traceback ( most recent call last)
<ipython-input-1-b8d5fc013374> in <module>()
      3 t = ( 0, 1, 2, 3, 4, 5 )
      4 print(t)
----> 5 t[0]=3
      6 print(t)

TypeError: 'tuple' object does not support item assignment.

If we observe the above program, changing the value present at index 0, 0 to 3 is impossible because tuples are immutable.

Let us do the same program with the list then try to observe the changes correspondingly:

Python program to change the value inside the list

Output

<class 'list'>
[0, 1, 2, 3, 4, 5]
[3, 1, 2, 3, 4, 5]

If we compare the above example of the tuple with the list, we can easily update the data item in the list compared to the tuples. In tuples, if we want to make any changes to the data items, we cannot do so because the tuple data structure does not support the changes in its data item.

Now let's try to delete any data item from the tuple:

Python program to delete the value inside the tuple

Output

<class 'tuple'>
(12, 13, 15, 2, 7, 9)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-53a7b72542d3> in <module>()
      2 print(type(t))
      3 print(t)
----> 4 del t[0]

TypeError: 'tuple' object doesn't support item deletion.

In the above example of the tuple, we are trying to perform the delete operation into the tuples by deleting the value present at the index 0. Still, if we observe that we cannot perform the delete operation, the tuple data structure does not support the delete operation.

Let us compare the tuple data type with the list, taking the above example of deletion as a test case.

Python program to delete the value inside the list

Output

<class 'list'>
[12, 13, 15, 2, 7, 9]
[13, 15, 2, 7, 9]

In the above example of the list, we can easily observe that we can delete the particular data item present at the particular index position. We cannot delete the item present at the particular position in the tuple data type compared to the tuples. As in the above example, we are trying to delete the data item present at the index 0, and we can successfully delete it.

Let us try to delete the whole tuple data structure in a program.

Python program to delete the whole tuple

Output

<class 'tuple'>
(12, 13, 15, 2, 7, 9)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-4c727775f4a1> in <module>()
      5 print(t)
      6 del t
----> 7 print(t)
      8 

NameError: name 't' is not defined

Here in the above example, if we try to observe that we can be easily able to delete the whole tuple from the program, but we are unable to delete the part of the tuple, it means we are unable to delete the particular value present at the particular index position of the tuple. Hence deleting the whole tuple is successfully executed but deleting the particular value from the tuple is impossible.

Now let us try to add the data element into the tuple using the append function:

Python program to add the data element inside the tuple

Output

<class 'tuple'>
(12, 13, 15, 2, 7, 9)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-170ba378b342> in <module>()
      4 print(type(t))
      5 print(t)
----> 6 t.append(9)
      7 print(t)
      8 

AttributeError: 'tuple' object has no attribute 'append.'

In the above example, if we want to add the data elements into the tuples using the append function, we cannot add the data element into the tuple. It implies that tuples do not support the append function into it.

Now let us execute the same operation into the list:

Python program to add the data item inside the tuple

Output

<class 'list'>
[0, 1, 2, 3, 4, 5, 9]
[3, 1, 2, 3, 4, 5, 9]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA