{"id":4446,"date":"2022-06-03T19:31:00","date_gmt":"2022-06-04T00:31:00","guid":{"rendered":"https:\/\/wanderin.dev\/?p=4446"},"modified":"2024-10-06T10:10:07","modified_gmt":"2024-10-06T15:10:07","slug":"python-tuples","status":"publish","type":"post","link":"https:\/\/wanderin.dev\/python-interview\/python-tuples\/","title":{"rendered":"Python Tuples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-about-this-article\">About this article<\/h2>\n\n\n\n<p>In this article, I&#8217;ll explore the <strong>tuple<\/strong>, one of 4 built-in types used to store collections in Python.&nbsp;<\/p>\n\n\n\n<p>This post is the second article in a miniseries exploring the topic of built-in collection types in Python. I based the series on my notes while studying for a Python technical interview.<\/p>\n\n\n\n<p>For quick access, here is the list of posts on this series:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-lists\/\">Python Lists<\/a><\/li><li>Python Tuples (this article)<\/li><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-sets\/\">Python Sets<\/a><\/li><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-dictionaries\/\">Python Dictionaries<\/a><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-introduction-to-tuples\">Introduction to tuples<\/h2>\n\n\n\n<p>In Python, a tuple is a data type used for storing a collection of objects. Tuples have the following characteristics:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuples-have-an-order\">Tuples have an order<\/h3>\n\n\n\n<p>Tuples preserve the order of their elements. Two tuples with the same elements but in different order are not equal:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/00fe215fc11af31ec7a9cc0a139455ee.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuples-can-contain-any-arbitrary-object\">Tuples can contain any arbitrary object<\/h3>\n\n\n\n<p>Tuple elements can be of any data type. The example below shows a tuple containing integers, floats, boolean, strings, dictionaries, and other tuple.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/635abcacb0bb42cc70f130ec52fa2671.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuples-can-contain-duplicates\">Tuples can contain duplicates<\/h3>\n\n\n\n<p>The same element can exist at different indexes in a tuple. For example, the tuple below contains the integer 1 at positions 0 and 2.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/44d2e6bd2c2ac42926681f92a0160c8d.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuples-are-immutable\">Tuples are immutable<\/h3>\n\n\n\n<p>You can&#8217;t change, add or remove elements after creating a tuple.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/f7fdde0acbeab21606487386a74b80a0.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-tuple\">Creating a tuple<\/h2>\n\n\n\n<p>You can create a tuple using a tuple literal by wrapping its elements in round brackets:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/43ee291626c87766a0f5e9ea90daeaf9.js\"><\/script>\n\n\n\n<p>When creating a tuple with round brackets, if the tuple has only one element, you should use a trailing comma:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/6ae3b350c2d20f0cac75a1207f0e72dd.js\"><\/script>\n\n\n\n<p>You can also use the tuple () constructor:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/ad350bc47f1b7cc9d8f73469b27f6543.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-accessing-elements-from-a-tuple\">Accessing elements from a tuple<\/h2>\n\n\n\n<p>You can access an element from a tuple by using the element index. The index indicates the position of the element within the tuple. Indexes in sequential data types in Python start at 0.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/be7aca8f34ed007649a5362a83efd089.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tuple-slicing\">Tuple slicing<\/h2>\n\n\n\n<p>By slicing a tuple, you can obtain a new tuple that is a sub-tuple of the original. The syntax for slicing is: &nbsp;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/e131a245f3b92d688e05903e2d74c349.js\"><\/script>\n\n\n\n<p>Which would return a sub-tuple from index = start to index = end -1 using the given step (or 1 if no step is given).<\/p>\n\n\n\n<p>Let&#8217;s look a some examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-implied-step\">Slicing with implied step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/e6376cb953aa31dc6dd228af284c7adb.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-specified-step\">Slicing with specified step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/b9ff31dea6c24982455489e63f512631.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-negative-step\">Slicing with negative step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/685c05f2fc62e0cb2901269de3ed0770.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-from-the-beginning-of-the-tuple\">Slicing from the beginning of the tuple<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/9c17a2fecd00c36e3125d805701a306c.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-to-the-end-of-the-tuple\">Slicing to the end of the tuple<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/c3c985ac45d1a5bbbe219f03a4edc9b8.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-negative-indexes\">Slicing with negative indexes<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/41a7719e7c0aea192f9e1814ef1a49fd.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tuple-built-in-methods\">Tuple built-in methods<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuple-index-x\">tuple.index(x)<\/h3>\n\n\n\n<p>The .index(x) method returns the index of the first occurrence of x in the tuple. This method has a linear time complexity of O(n), where n is the length of the tuple.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/ac32c6e4588dcda6828a48d489ca2826.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tuple-count-x\">tuple.count(x)<\/h3>\n\n\n\n<p>The .count(x) method returns the number of times x appears in the tuple. This method has a linear time complexity of O(n), where n is the length of the tuple.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/b01ec23206b24b876c0486c9707cedd9.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tuple-unpacking\">Tuple unpacking<\/h2>\n\n\n\n<p>As with other iterables in Python, you can unpack the elements of a tuple into individual variables in one line:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/25b0fb1f4170ab30ca9a8c78cb42ee5a.js\"><\/script>\n\n\n\n<p>The only requirement is that the number of variables on the left of the assignment operator is equal to the length of the tuple.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-lists-or-tuples\">Lists or tuples?<\/h2>\n\n\n\n<p>So, what should you use, a list or a tuple? The short answer is to use a tuple if the values in the collection should remain constant over the program&#8217;s life. That way, you&#8217;ll enjoy faster execution and avoid bugs introduced by accidentally modifying the values in the collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>This article covered Python tuples, a built-in data type you will most likely use in any non-trivial coding project. We covered the main characteristics of tuples, the topic of slicing, the tuple built-in methods with their time complexity, and tuple unpacking.<\/p>\n\n\n\n<p>We also gave a rule of thumb for when to use tuples instead of lists: always use tuples if the values in the collection should remain constant over the program&#8217;s life.<\/p>\n\n\n\n<p>I hope the information covered will help you take advantage of Python tuples in your next programming project.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/javierfeliu.me\/python-interview\/python-sets\/\">next article<\/a> explores Python sets.  See you there!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.w3schools.com\/python\/python_tuples_unpack.asp\" target=\"_blank\" rel=\"noreferrer noopener\">W3Schools &#8211; Python Tuples<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#tuples-and-sequences\" target=\"_blank\" rel=\"noreferrer noopener\">Python Documentations &#8211; Tuples and Sequences<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/realpython.com\/python-lists-tuples#python-tuples\" target=\"_blank\" rel=\"noreferrer noopener\">Real Python &#8211; Lists and Tuples in Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.tutorialkart.com\/python\/python-slice-a-tuple\/\" target=\"_blank\" rel=\"noreferrer noopener\">TutorialKart &#8211; Slice a Tuple in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explores the tuples, one of four built-in collection types in Python. We review the properties of tuples, their methods with their time complexity , the topic of slicing, and tuple unpacking.<\/p>\n<p>This article is the second in a miniseries exploring built-in collection types in Python.<\/p>\n","protected":false},"author":152069353,"featured_media":4614,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[4504528,4504527],"tags":[4504533,4504531],"class_list":["post-4446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-built-in-collections","category-python-interview","tag-built-in-collections","tag-python-interview"],"jetpack_featured_media_url":"https:\/\/wanderin.dev\/wp-content\/uploads\/2022\/06\/2.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/4446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/users\/152069353"}],"replies":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/comments?post=4446"}],"version-history":[{"count":43,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/4446\/revisions"}],"predecessor-version":[{"id":4812,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/4446\/revisions\/4812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/media\/4614"}],"wp:attachment":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/media?parent=4446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/categories?post=4446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/tags?post=4446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}