Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are tuples immutable?
#3
more from the docs

Quote:It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples).

In your example you change mutable object - list, within immutable tuple

>>> spam = (1, [2, 3])
>>> id(spam[1])
139977080078024
>>> spam[1][1] = 4
>>> spam
(1, [2, 4])
>>> id(spam[1]) # still same object id
139977080078024
>>> spam[1] = 'eggs'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Are tuples immutable? - by python_user_n - Jan-06-2019, 01:00 PM
RE: Are tuples immutable? - by perfringo - Jan-06-2019, 01:53 PM
RE: Are tuples immutable? - by buran - Jan-06-2019, 02:27 PM
RE: Are tuples immutable? - by python_user_n - Jan-06-2019, 02:41 PM
RE: Are tuples immutable? - by buran - Jan-06-2019, 03:06 PM
RE: Are tuples immutable? - by ichabod801 - Jan-06-2019, 03:28 PM
RE: Are tuples immutable? - by python_user_n - Jan-06-2019, 04:24 PM
RE: Are tuples immutable? - by ichabod801 - Jan-06-2019, 04:39 PM
RE: Are tuples immutable? - by micseydel - Jan-06-2019, 04:46 PM
RE: Are tuples immutable? - by python_user_n - Jan-06-2019, 05:21 PM
RE: Are tuples immutable? - by perfringo - Jan-07-2019, 08:04 AM
RE: Are tuples immutable? - by python_user_n - Jan-07-2019, 08:08 AM
RE: Are tuples immutable? - by buran - Jan-07-2019, 11:12 AM
RE: Are tuples immutable? - by python_user_n - Jan-07-2019, 01:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  immutable types Skaperen 2 2,167 Jul-09-2021, 01:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020