Is tuple mutable in Python?

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 tuple 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

Output:

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

print(tup1)

Output: [(1, 2), 4]

Modifying a tuple inside the list

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

tup1[0][0] = 1


print(tup1)

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.