{"id":4872,"date":"2021-05-01T15:02:52","date_gmt":"2021-05-01T09:32:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=4872"},"modified":"2021-11-22T18:42:58","modified_gmt":"2021-11-22T13:12:58","slug":"python-tuple-append-insert-modify-delete-elements-in-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/","title":{"rendered":"Python Tuple : Append , Insert , Modify & delete elements in Tuple"},"content":{"rendered":"
This article is about how we can append, insert, modify and delete elements in Tuple.<\/p>\n
As we know a tuple in Python stores ordered and immutable objects. It is one of data type which stores multiple items in a single variable where all the elements are placed inside parentheses () and separated by commas.<\/p>\n
Syntax : sample_tuple = (element1, element2, element3, ...)<\/pre>\nAs tuple is immutable so once created values can not be changed. Still if we want to modify the existing tuple, then in that case we have to create a new tuple with updated elements only from the existing tuple. So let’s start exploring the topic to know how we can append, insert, modify and delete elements in Tuple.<\/p>\n
Append an element in Tuple at end :<\/h3>\n
If we have a tuple and to append an element into it , then we will create copy of the existing tuple first then we will append the new element by using
+<\/code> operator.<\/p>\n
So , let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\n# A tuple created\r\ntuple_Obj = (1 , 3, 4, 2, 5 )\r\n\r\n#printing old tuple\r\nprint(\"Old tuple is :\")\r\nprint(tuple_Obj)\r\n\r\n# Appending 9 at the end of tuple\r\ntuple_Obj = tuple_Obj + (9 ,)\r\n\r\n#printing new tuple\r\nprint(\"After appending new tuple is :\")\r\nprint(tuple_Obj)<\/pre>\nOutput\u00a0 :\r\nOld tuple is :\r\n(1 , 3, 4, 2, 5 )\r\nAfter appending new tuple is :\r\n(1 , 3, 4, 2, 5, 9 )<\/pre>\nInsert an element at specific position in tuple :<\/h3>\n
If we want to insert a specific element at particular index, then we have to create a new tuple by slicing the existing tuple and copying elements of old tuple from it.<\/p>\n
Suppose we have to insert at
index n<\/code> then we have to create two sliced copies of existing tuple from (0 to n) and (n to end). Like<\/p>\n
\n# containing elements from 0 to n-1 \r\ntuple_Obj <\/span>[<\/span> : n<\/span>] \r\n<\/span><\/span># containing elements from n to end \r\ntuple_Obj <\/span>[<\/span>n : <\/span>]<\/span><\/pre>\n<\/div>\n
So, let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\n# A tuple created\r\ntuple_Obj = (1 , 3, 4, 2, 5 )\r\n\r\n#printing old tuple\r\nprint(\"Old tuple is :\")\r\nprint(tuple_Obj)\r\n\r\nn = 2\r\n# Insert 9 in tuple at index 2\r\ntuple_Obj = tuple_Obj[ : n ] + (9 ,) + tuple_Obj[n : ]\r\n\r\n#printing new tuple\r\nprint(\"After appending new tuple is :\")\r\nprint(tuple_Obj)\r\n<\/pre>\nOutput\u00a0 :\r\nOld tuple is :\r\n(1 , 3, 4, 2, 5 )\r\nAfter appending new tuple is :\r\n(1 , 3, 9, 4, 2, 5 )<\/pre>\nModify \/ Replace the element at specific index in tuple :<\/h3>\n
If we want to replace the element at
index n<\/code> in tuple then we have to use the same slicing logic as we used in the above example, But in this we have to slice the tuple from from (0 to n-1) and (n+1 to end) , as we will replace the element at
index n<\/code>, so after replacing we are copying the elements again from
n+1 index<\/code> of the old tuple.<\/p>\n
So, let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\n# A tuple created\r\ntuple_Obj = (1 , 3, 4, 2, 5 )\r\n\r\n#printing old tuple\r\nprint(\"Old tuple is :\")\r\nprint(tuple_Obj)\r\n\r\nn = 2\r\n# Insert 'program' in tuple at index 2\r\ntuple_Obj = tuple_Obj[ : n] + ('program' ,) + tuple_Obj[n + 1 : ]\r\n\r\n#printing new tuple\r\nprint(\"After appending new tuple is :\")\r\nprint(tuple_Obj)\r\n<\/pre>\nOutput :\r\nOld tuple is : \r\n(1 , 3, 4, 2, 5 ) \r\nAfter appending new tuple is : \r\n(1 , 3, 'program', 2, 5 )<\/pre>\nDelete an element at specific index in tuple :<\/h3>\n
If we want to delete an element at
index n<\/code> in tuple then we have to use the same slicing logic as\u00a0 we used in the above example, means we will slice the tuple from from (0 to n-1) and (n+1 to end) , like<\/p>\n
# containing elements from 0 to n-1 <\/span>\r\ntuple_Obj <\/span>[<\/span> : n<\/span>] <\/span><\/span>\r\n# containing elements from n to end \r\ntuple_Obj <\/span>[<\/span>n+1 : <\/span>]<\/span><\/pre>\n
So, let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\n# A tuple created\r\ntuple_Obj = (1 ,3, 'program', 4, 2, 5 )\r\n\r\n#printing old tuple\r\nprint(\"Old tuple is :\")\r\nprint(tuple_Obj)\r\n\r\nn = 2\r\n# Deleting the element at index 2 \r\ntuple_Obj = tuple_Obj[ : n ] + tuple_Obj[n+1 : ]\r\n\r\n#printing new tuple\r\nprint(\"After appending new tuple is :\")\r\nprint(tuple_Obj)<\/pre>\nOutput : \r\nOld tuple is : (1 , 3, 'program',\u00a0 4, 2, 5 ) \r\nAfter appending new tuple is : (1 , 3, 4, 2, 5 )<\/pre>\n","protected":false},"excerpt":{"rendered":"How to append, insert, modify and delete elements in Tuple in Python ? This article is about how we can append, insert, modify and delete elements in Tuple. As we know a tuple in Python stores ordered and immutable objects. It is one of data type which stores multiple items in a single variable where …<\/p>\n