1

Why python tuple cannot changes element ? I know that how change tuple. Giving me a reason for cannot changes element.

like below:

a= (1, 2, 3,)

>

a[0] = 10 Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
Danielsss
  • 11
  • 1
  • 4

2 Answers2

2

tuples, like strings, are immutable objects in that their values cannot be changed once they have been created. You usually use tuples when you want to store a list of values that you wont be editing, maybe they are constants. If you will be editing, resizing or appending elements to the tuple, use lists instead.

You can do it either as follows:

a = (10, a[1], a[2])

Or using lists. Lists are much more dynamic and allow item assignment and editing.

For example:

>>> a = [1,2,3]
>>> a[0] = 10
>>> a
[10,2,3]
sshashank124
  • 29,826
  • 8
  • 62
  • 75
0

As you can read here, tuples are immutable sequence types. This means you cannot change them.

Tim
  • 39,651
  • 17
  • 123
  • 137