Python Program to Edit objects inside tuple

PythonServer Side ProgrammingProgramming

When it is required to edit the objects inside a tuple, simple indexing can be used.

A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

Below is a demonstration for the same −

Example

 Live Demo

my_tuple = (45, 67, [35, 66, 74], 89, 100)
print("The tuple is : ")
print(my_tuple)
my_tuple[2][1] = 63
print("The tuple after changes is : ")
print(my_tuple)

Output

The tuple is :
(45, 67, [35, 66, 74], 89, 100)
The tuple after changes is :
(45, 67, [35, 63, 74], 89, 100)

Explanation

  • A tuple of list is defined, and is displayed on the console.
  • Since a tuple is immutable, but if the tuple contains a list, it can be varied.
  • This is because the list is actually a mutable type.
  • This can be done by accessing the index of the elements in the list.
  • It is then displayed as output on the console.
raja
Published on 11-Mar-2021 12:44:34
Advertisements