Skip to content

Is tuple mutable in Python?

  • by

No Python tuples are not mutable data type. Tuples are immutable, meaning that once a tuple has been created, the items in it can’t change.

Example tuples are not mutable in Python

Simple example code once a tuple has been created you can’t change the order, append, delete or insert the entry.

tup1 = (1, 2, 3)

tup1[1] = 4


Is tuple mutable in Python

Is a list of tuples mutable or is it immutable in Python?

Answer: A list of tuples can be modified as a list is a mutable entity. However, an individual tuples cannot be modified. Consider the below example.

Modifying the list itself

tup1 = [(1, 2), (3, 4)]

tup1[1] = 4


Output: [(1, 2), 4]

Modifying a tuple inside the list

tup1 = [(1, 2), (3, 4)]

tup1[0][0] = 1


Output: TypeError: ‘tuple’ object does not support item assignment

Do comment if you have any doubts and suggestions on this Python tuple tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *