{"id":3324,"date":"2022-11-25T13:31:47","date_gmt":"2022-11-25T13:31:47","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3324"},"modified":"2022-11-25T13:31:48","modified_gmt":"2022-11-25T13:31:48","slug":"are-tuples-mutable-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/are-tuples-mutable-in-python\/","title":{"rendered":"Are Tuples Mutable in Python"},"content":{"rendered":"<p style=\"text-align: center;\">Are Tuples Mutable in Python<\/p>\n<p>As of now, we are familiar with some things in relation to the tuple, like how we can create tuples, and how we can access the individual tuple elements as well. Now, we need to have a look at whether or not are tuples mutable. Basically, we need to check if we can access the tuple elements and if can even we change them. Let\u2019s have a look at the below program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3326 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE.jpg\" alt=\"\" width=\"862\" height=\"156\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE.jpg 862w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-300x54.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-768x139.jpg 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/p>\n<h2 style=\"text-align: center;\">Are Tuples Mutable in Python<\/h2>\n<p>As you can see, in the above program, we are having a tuple, and then in the next line, we are trying to access the individual tuple element, and perform some assignment. Well, we go into errors if we do so. The thing is that the tuples are immutable, which means that we cannot alter, add, or remove the elements in the tuple. If we try to do so directly on a tuple element, we get into an error. The error simply says that &#8211; TypeError: &#8216;tuple&#8217; object does not support item assignment. So, we cannot simply directly make changes here.<\/p>\n<p>But now, if we are required to make changes to the tuple, we just saw that we are not able to do so. But there is something else that we can do. In order to make some changes to the tuple, we can first convert that tuple to a list, and then make those changes on the list, and then change it back to a tuple.<\/p>\n<p>Let\u2019s have a look at the below program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3327 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-1.jpg\" alt=\"\" width=\"845\" height=\"152\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-1.jpg 845w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-1-300x54.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ARE-TUPLES-MUTABLE-1-768x138.jpg 768w\" sizes=\"auto, (max-width: 845px) 100vw, 845px\" \/><\/p>\n<p>As you can see, in the above program, we have a tuple, and we know that we cannot make changes in the tuple now, so we first convert the tuple to a list, and then try to make the changes, and then convert it back to a tuple. When we print the tuple, we can simply see that we have the tuple with the respective changes.<\/p>\n<p>So, if you want to make some changes to the tuple, we cannot directly do so, but we can convert it into a list, and then make the changes and convert it back to a tuple.<\/p>\n<p>The thing is that we can even complicate things, by having a tuple inside a tuple, or some other sequence like list, or string in the tuple. This can be according to the requirement. We can have multiple data of different datatype, and also, we can have duplicate values. So, we can make use of the tuples in our programs as and when required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are Tuples Mutable in Python As of now, we are familiar with some things in relation to the tuple, like how we can create tuples, and how we can access the individual tuple elements as well. Now, we need to have a look at whether or not are tuples mutable. Basically, we need to check [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3324","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3324"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3324\/revisions"}],"predecessor-version":[{"id":4091,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3324\/revisions\/4091"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}