{"id":8179,"date":"2024-03-19T06:55:29","date_gmt":"2024-03-19T06:55:29","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8179"},"modified":"2024-03-20T06:35:21","modified_gmt":"2024-03-20T06:35:21","slug":"python-update-tuples","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-update-tuples\/","title":{"rendered":"Python Update Tuples"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#python-update-tuples\">Python Update Tuples<\/a><\/li><li ><a href=\"#what-is-tuple-in-python\">What is Tuple in Python? <\/a><\/li><li ><a href=\"#how-do-you-change-the-tuple-values-in-python\">How do you change the Tuple Values in Python?<\/a><\/li><li ><a href=\"#what-are-the-several-ways-to-add-the-item-to-the-python-tuple\">What are the Several ways to add the item to the Python Tuple?<\/a><\/li><li ><a href=\"#what-are-the-examples-for-converting-the-list-back-into-the-python-tuple\">What are the examples for converting the list back into the Python Tuple?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-update-tuples-fa-qs\">Python &#8211; Update Tuples -FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-update-tuples\">Python Update Tuples<\/h2>\n\n\n\n<p><strong><a href=\"https:\/\/www.skillvertex.com\/blog\/python-interview-questions\/\" data-type=\"post\" data-id=\"3614\">Python <\/a><\/strong>is versatile and is known as a powerful <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-given-languages-is-not-commonly-used-for-ai\/\" data-type=\"post\" data-id=\"2778\">programming language. <\/a>It will also provide rich <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-create-multiset-data-structure-in-python\/\" data-type=\"post\" data-id=\"3341\">data structures f<\/a>or monitoring several types of information. Check out this article to learn more about the Python Update Tuples.<\/p>\n\n\n\n<p>Whereas, <a href=\"https:\/\/www.skillvertex.com\/blog\/python-access-tuple-items\/\" data-type=\"post\" data-id=\"8169\">tuples <\/a>are ordered collections with unique characteristics. Tuples are <a href=\"https:\/\/www.skillvertex.com\/blog\/dictionary-is-mutable-or-immutable\/\" data-type=\"post\" data-id=\"1615\">immutable <\/a>and once the meaning is made, it can&#8217;t be altered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-tuple-in-python\">What is Tuple in Python? <\/h2>\n\n\n\n<p>In Python, A <strong><a href=\"https:\/\/www.skillvertex.com\/blog\/python-tuples\/\" data-type=\"post\" data-id=\"8162\">tuple <\/a><\/strong>is referred to as an ordered, immutable sequence of elements. It also has a data<a href=\"https:\/\/www.skillvertex.com\/blog\/data-analyst-interview-question-and-answer\/\" data-type=\"post\" data-id=\"3730\"> <\/a>structure that will allow you to store the multiple values of different types together.<\/p>\n\n\n\n<p>Furthermore, <strong>tuples <\/strong>will work similarly to the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-methods\/\" data-type=\"post\" data-id=\"8139\">list <\/a>in Python. In the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\">syntax <\/a>of tuples,  it will consist of elements that are surrounded by the parenthesis() and are separated with commas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-you-change-the-tuple-values-in-python\">How do you change the Tuple Values in Python?<\/h2>\n\n\n\n<p>Check out the <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-examples\/\" data-type=\"post\" data-id=\"4337\">example <\/a>to learn how to convert the tuple into the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Original tuple\nx = (\"apple\", \"banana\", \"cherry\")\n\n# Convert tuple to list\ny = list(x)\n\n# Replace the second element of the list with 'mango'\ny&#91;1] = 'mango'\n\n# Display the converted list\nprint(\"Original Tuple:\")\nprint(x)\nprint(\"\\nConverted List:\")\nprint(y)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Tuple:\n('apple', 'banana', 'cherry')\n\nConverted List:\n&#91;'apple', 'mango', 'cherry']\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-several-ways-to-add-the-item-to-the-python-tuple\">What are the Several ways to add the item to the Python Tuple?<\/h2>\n\n\n\n<p>In <a href=\"https:\/\/www.skillvertex.com\/blog\/python-tutorial\/\" data-type=\"post\" data-id=\"6852\">Python<\/a>, <strong>tuples <\/strong>are mostly immutable and do not have a <strong>built-in append()<\/strong>  method. Some of the ways to add the items  to the tuple are provided below:<\/p>\n\n\n\n<p><strong>a. Convert into the list:<\/strong><\/p>\n\n\n\n<p>Tuples can be changed into the list by adding the item and then, turning it back to the tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Original tuple\nx = (\"apple\", \"banana\", \"cherry\")\n\n# Convert tuple to list\ny = list(x)\n\n# Add \"grapes\" to the list\ny.append(\"grapes\")\n\n# Convert the modified list back to tuple\nx_modified = tuple(y)\n\n# Display the modified tuple\nprint(\"Original Tuple:\")\nprint(x)\nprint(\"\\nModified Tuple with 'grapes' added:\")\nprint(x_modified)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Tuple:\n('apple', 'banana', 'cherry')\n\nModified Tuple with 'grapes' added:\n('apple', 'banana', 'cherry', 'grapes')\n<\/code><\/pre>\n\n\n\n<p><strong>b. Add tuple to the tuple<\/strong><\/p>\n\n\n\n<p>In Python, it is possible to add tuples to tuples. Such as adding an item, creating a new tuple with the item, and then adding it to the existing tuple as shown in the example below.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Original tuple\nx = (\"apple\", \"banana\", \"cherry\")\n\n# Create a new tuple with the value \"kiwi\"\nnew_tuple = (\"kiwi\",)\n\n# Concatenate the original tuple and the new tuple\ncombined_tuple = x + new_tuple\n\n# Display the combined tuple\nprint(\"Combined Tuple:\")\nprint(combined_tuple)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Combined Tuple:\n('apple', 'banana', 'cherry', 'kiwi')\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-examples-for-converting-the-list-back-into-the-python-tuple\">What are the examples for converting the list back into the Python Tuple?<\/h2>\n\n\n\n<p>It is important to remember that removing items is not possible in Tuples of Python. Let us look into the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Original tuple\nx = (\"apple\", \"banana\", \"cherry\")\n\n# Convert tuple to list\ny = list(x)\n\n# Remove \"banana\" from the list\nif \"banana\" in y:\n    y.remove(\"banana\")\n\n# Convert the modified list back to tuple\nx_modified = tuple(y)\n\n# Display the modified tuple\nprint(\"Original Tuple:\")\nprint(x)\nprint(\"\\nModified Tuple with 'banana' removed:\")\nprint(x_modified)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Tuple:\n('apple', 'banana', 'cherry')\n\nModified Tuple with 'banana' removed:\n('apple', 'cherry')\n<\/code><\/pre>\n\n\n\n<p>Another way to delete the items in the tuple is to delete it completely with the help of the del keyword:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thistuple = (\"apple\", \"banana\", \"mango\")\ndel thistuple\nprint(thistuple) #this will raise an error because the tuple no longer exists<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\n  File \"main.py\", line 3, in &lt;module&gt;\n    print(thistuple)  # this will raise an error because the tuple no longer exists\nNameError: name 'thistuple' is not defined<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In Python, tuples are like lists but with one crucial difference: they are immutable, meaning once created, their elements cannot be changed. However, there are still ways to update tuples indirectly. Beginners can improve their skills and knowledge on changing or adding the items of tuples in  Python. Several examples are illustrated in this article for their reference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-update-tuples-fa-qs\">Python &#8211; Update Tuples -FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710742643843\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.Can we update the tuple in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In Python, the value cannot be changed after the tuple is formed.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710742650068\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.Can you mutate a tuple in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.No, it doesn&#8217;t have any private attribute to mutate the tuple in Python.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710742657754\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.How do you append to a tuple?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A tuple can be appended with the help of the += operator.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python Update Tuples Python is versatile and is known as a powerful programming language. It will also provide rich data structures for monitoring several types of information. Check out this article to learn more about the Python Update Tuples. Whereas, tuples are ordered collections with unique characteristics. Tuples are immutable and once the meaning is &#8230; <a title=\"Python Update Tuples\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-update-tuples\/\" aria-label=\"More on Python Update Tuples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,932,916,983,984],"class_list":["post-8179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-functions","tag-python-programming","tag-python-update-tuples","tag-tuples","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8179"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8179\/revisions"}],"predecessor-version":[{"id":8343,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8179\/revisions\/8343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8181"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}