{"id":3334,"date":"2022-11-25T14:45:46","date_gmt":"2022-11-25T14:45:46","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3334"},"modified":"2022-11-25T14:45:48","modified_gmt":"2022-11-25T14:45:48","slug":"iterate-through-a-tuple-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/iterate-through-a-tuple-in-python\/","title":{"rendered":"Iterate Through A Tuple In Python"},"content":{"rendered":"<p style=\"text-align: center;\">Iterate Through A Tuple In Python<\/p>\n<p>We have been exploring tuples, and we have seen many different things, like how to create tuples, how to access the elements from the tuple, and much more. Now, we are going to explore how we can iterate through the tuple. Basically, iterating through a tuple simply can be understood as going through the tuple elements. At times, we might need to iterate through the tuple, and one of the ways to do this is by using loops. Let\u2019s have a look at the below program, which demonstrates how we can iterate through the tuple using a loop. We are going to make use of a for loop here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3335 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON.jpg\" alt=\"\" width=\"912\" height=\"127\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON.jpg 912w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-300x42.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-768x107.jpg 768w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/p>\n<h2 style=\"text-align: center;\">Iterate Through A Tuple In Python<\/h2>\n<p>As you can see in the above program, we have a simple tuple, and then we are trying to loop through the tuple. Here, the thing is that in every iteration, the element variable has one item from the tuple. So, in the first iteration, the element has 2, then in the next iteration, it has 9, and so on. Let\u2019s have a look at the output of the above program.<\/p>\n<p>2<br \/>\n9<br \/>\n3.1<br \/>\nGyaniPandit<br \/>\nTrue<\/p>\n<p>As you can see, we have one item in each iteration from the tuple, with the element variable. So,<\/p>\n<p>whenever we need to loop through the tuple, we can do so like we did in the previous example. But here, again, assigning any values to the element variable in the loop, won&#8217;t affect the tuple elements.<\/p>\n<p>We can also make use of the range function, to access the elements with the indexes, but still, if we try to access them with the indexes, and then try to make changes in the tuple directly, we willget into an error. Let\u2019s have a look at a simple program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3336 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-1.jpg\" alt=\"\" width=\"912\" height=\"125\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-1.jpg 912w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-1-300x41.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-1-768x105.jpg 768w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/p>\n<p>As you can see in the above program, we have a simple tuple, and we are trying to loop through the tuple. This time, we are making use of the range function, and we are trying to access the elements with the help of their indexes. Also, we are making use of the len function, to get the length of the tuple. In this way, we are able to loop through the tuple, with help of the indexes of the elements.<\/p>\n<p>You can also make use of the while loop, to iterate through the tuple. Let\u2019s have a look at a simple program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3337 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-2.jpg\" alt=\"\" width=\"933\" height=\"164\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-2.jpg 933w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-2-300x53.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ITERATING-THROUGH-TUPLES-IN-PYTHON-2-768x135.jpg 768w\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" \/><\/p>\n<p>As you can see in the above program, we have made use of the while loop, to loop through the tuple. Remember that here, we first have the index as zero, and then we are executing while loop, till the end of the tuple. In the while loop, we are printing the element, use the index, and then we are incrementing the index.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Iterate Through A Tuple In Python We have been exploring tuples, and we have seen many different things, like how to create tuples, how to access the elements from the tuple, and much more. Now, we are going to explore how we can iterate through the tuple. Basically, iterating through a tuple simply can be [&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-3334","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\/3334","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=3334"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3334\/revisions"}],"predecessor-version":[{"id":4093,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3334\/revisions\/4093"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}