{"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":"

How to append, insert, modify and delete elements in Tuple in Python ?<\/h2>\n

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>\n

As 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>\n
Output\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>\n

Insert 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>\n
Output\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>\n

Modify \/ 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>\n
Output :\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>\n

Delete 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>\n
Output : \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

Python Tuple : Append , Insert , Modify & delete elements in Tuple<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 … Python Tuple : Append , Insert , Modify & delete elements in Tuple Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-01T09:32:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:12:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/\",\"name\":\"Python Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-05-01T09:32:52+00:00\",\"dateModified\":\"2021-11-22T13:12:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tuple : Append , Insert , Modify & delete elements in Tuple\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python Tuple : Append , Insert , Modify & delete elements in Tuple\",\"datePublished\":\"2021-05-01T09:32:52+00:00\",\"dateModified\":\"2021-11-22T13:12:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage\"},\"wordCount\":387,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/","og_locale":"en_US","og_type":"article","og_title":"Python Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs","og_description":"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 … Python Tuple : Append , Insert , Modify & delete elements in Tuple Read More »","og_url":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-05-01T09:32:52+00:00","article_modified_time":"2021-11-22T13:12:58+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage","url":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/","name":"Python Tuple : Append , Insert , Modify & delete elements in Tuple - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-05-01T09:32:52+00:00","dateModified":"2021-11-22T13:12:58+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Tuple : Append , Insert , Modify & delete elements in Tuple"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python Tuple : Append , Insert , Modify & delete elements in Tuple","datePublished":"2021-05-01T09:32:52+00:00","dateModified":"2021-11-22T13:12:58+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#webpage"},"wordCount":387,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-tuple-append-insert-modify-delete-elements-in-tuple\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4872"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=4872"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4872\/revisions"}],"predecessor-version":[{"id":4940,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4872\/revisions\/4940"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=4872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=4872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=4872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}