{"id":449,"date":"2026-10-07T19:02:39","date_gmt":"2026-10-07T19:02:39","guid":{"rendered":"http:\/\/askpython.com\/?p=449"},"modified":"2026-10-07T19:02:40","modified_gmt":"2026-10-07T19:02:40","slug":"python-tuple","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/tuple\/python-tuple","title":{"rendered":"Python Tuple: How to Create and Use One"},"content":{"rendered":"<!-- article-type: tutorial -->\n<!-- receipt-backed-first-person -->\n\n<p class=\"wp-block-paragraph\">Sensor records can keep an identifier and reading in a fixed order. A Python tuple groups values in order and keeps its positions fixed after creation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ll show how to create a tuple and explain when its fixed positions suit a record.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n<p class=\"wp-block-paragraph\">A Python tuple is an ordered sequence whose positions cannot be reassigned after creation. Choose one for a fixed record, and choose a list when the group needs additions or removals.<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>Commas group tuple values. A one-item tuple needs a trailing comma.<\/li>\n<li>Indexing, slicing and unpacking let you read values without changing the tuple.<\/li>\n<li>Immutability does not guarantee hashability. Every value inside a tuple used as a dictionary key must be hashable too.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What Is a Python Tuple?<\/h2>\n\n\n<p class=\"wp-block-paragraph\">A tuple is an ordered Python sequence that can group a sensor identifier, reading and active flag while keeping their positions fixed.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A tuple does not store field names, so unpack its values into descriptive names because the reader has no labels to follow.<\/p>\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Choice<\/th><th>Can the sequence change?<\/th><th>Fits when<\/th><\/tr><\/thead><tbody>\n<tr><td>Tuple<\/td><td>No. Its positions cannot be replaced, added or removed.<\/td><td>The group has a stable order and fixed roles, such as a coordinate pair or a returned result.<\/td><\/tr>\n<tr><td>List<\/td><td>Yes. Items can be replaced, added or removed.<\/td><td>The collection grows, shrinks or changes as the program runs.<\/td><\/tr>\n<\/tbody><\/table><\/figure>\n\n\n<p class=\"wp-block-paragraph\">Tuples can mix object types and contain nested collections. Choose by the mutation rules the task needs, not by whether values share one type. See <a href=\"https:\/\/www.askpython.com\/python\/data-structures-in-python\">Data Structures in Python<\/a> for context on other collection types.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The pair (8, 8) keeps two indexed positions for equal values, unlike a set, which represents unique members.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A set represents unique members, and a dictionary maps keys to values. The standard library&#8217;s <a href=\"https:\/\/docs.python.org\/3\/library\/array.html\" target=\"_blank\" rel=\"noopener\">array type<\/a> is a mutable sequence whose type code restricts the stored values to a compatible basic type. Use it for typed numeric storage, not as a replacement for a record with mixed field types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create and Use a Python Tuple Step by Step<\/h2>\n\n\n<p class=\"wp-block-paragraph\">Use the comma to form a tuple, then read or unpack its values according to the record&#8217;s shape. These short runs cover creation, sequence access, unpacking and the edge between immutability and hashability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a tuple and check the comma<\/h3>\n\n\n<p class=\"wp-block-paragraph\">Commas form tuples, so parentheses are optional around values in a simple assignment. A single value still needs a trailing comma to form a one-item tuple.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; notranslate\">\npython3 create_tuple.py\n<\/pre><\/div>\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; notranslate\">\ncoordinates = (12, 7)\nsingle_item = (&quot;north&quot;,)\nempty = ()\nfrom_text = tuple(&quot;cat&quot;)\n\nprint(&quot;coordinates:&quot;, coordinates)\nprint(&quot;single item:&quot;, single_item, &quot;| type:&quot;, type(single_item).__name__)\nprint(&quot;empty:&quot;, empty)\nprint(&quot;from iterable:&quot;, from_text)\n<\/pre><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; notranslate\">\ncoordinates: (12, 7)\nsingle item: (&#039;north&#039;,) | type: tuple\nempty: ()\nfrom iterable: (&#039;c&#039;, &#039;a&#039;, &#039;t&#039;)\n<\/pre><\/div>\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"454\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output.png\" alt=\"AskPython terminal output showing Python tuple creation and the singleton comma\" class=\"wp-image-71977\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output-300x71.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output-1024x242.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output-768x182.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/create-tuple-output-1536x363.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption>Output from the captured run of python3 create_tuple.py.<\/figcaption><\/figure>\n\n\n<p class=\"wp-block-paragraph\">The empty parentheses create an empty tuple. The constructor in the last line consumes an iterable, so a string becomes a tuple of its characters. I ran this file and confirmed that the one-item value has type tuple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Read fields by index and slice<\/h3>\n\n\n<p class=\"wp-block-paragraph\">Tuple positions start at zero, while negative indexes count from the end. A slice returns a tuple from a range and excludes its stop position. I ran this example to check how the first and last positions map to the record.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; notranslate\">\npython3 inspect_tuple.py\n<\/pre><\/div>\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; notranslate\">\nrecord = (&quot;sensor-17&quot;, 23, True)\n\nprint(&quot;length:&quot;, len(record))\nprint(&quot;first field:&quot;, record&#x5B;0])\nprint(&quot;last field:&quot;, record&#x5B;-1])\nprint(&quot;middle slice:&quot;, record&#x5B;1:3])\n<\/pre><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; notranslate\">\nlength: 3\nfirst field: sensor-17\nlast field: True\nmiddle slice: (23, True)\n<\/pre><\/div>\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"454\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output.png\" alt=\"AskPython terminal output showing Python tuple length, indexing, negative indexing and slicing\" class=\"wp-image-71978\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output-300x71.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output-1024x242.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output-768x182.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/inspect-tuple-output-1536x363.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption>Output from the captured run of python3 inspect_tuple.py.<\/figcaption><\/figure>\n\n\n<p class=\"wp-block-paragraph\">The record has three positions, so its last item is at index negative one. A request beyond the tuple&#8217;s length raises an IndexError. Check the length or use a slice when the number of available fields can vary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Unpack a record into named values<\/h3>\n\n\n<p class=\"wp-block-paragraph\">Unpacking assigns sequence values to names in order, so match one target to each value for a fixed record. A starred target collects a variable middle section into a list. I ran this example to check the order of assigned values.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; notranslate\">\npython3 unpack_tuple.py\n<\/pre><\/div>\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; notranslate\">\nrecord = (&quot;sensor-17&quot;, 23, True, &quot;calibrated&quot;)\n\ndevice, reading, *details = record\n\nprint(&quot;device:&quot;, device)\nprint(&quot;reading:&quot;, reading)\nprint(&quot;remaining fields:&quot;, details)\n<\/pre><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; notranslate\">\ndevice: sensor-17\nreading: 23\nremaining fields: &#x5B;True, &#039;calibrated&#039;]\n<\/pre><\/div>\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"409\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output.png\" alt=\"AskPython terminal output showing tuple unpacking with a starred remainder\" class=\"wp-image-71979\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output-300x64.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output-1024x218.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output-768x164.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/unpack-tuple-output-1536x327.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption>Output from the captured run of python3 unpack_tuple.py.<\/figcaption><\/figure>\n\n\n<p class=\"wp-block-paragraph\">Without a starred target, the number of names must match the number of values. A function can also return several values for named assignment. The <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple-unpacking\">tuple unpacking guide<\/a> covers further forms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Check immutability and hashability<\/h3>\n\n\n<p class=\"wp-block-paragraph\">The final example tests field assignment, a mutable list inside a tuple and dictionary-key use. I ran it to check how the nested list and key behave.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; notranslate\">\npython3 tuple_edges.py\n<\/pre><\/div>\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; notranslate\">\npoint = (12, 7)\ntry:\n    point&#x5B;0] = 99\nexcept TypeError as error:\n    print(&quot;slot update:&quot;, type(error).__name__)\n\npoint = (99, point&#x5B;1])\nprint(&quot;replacement tuple:&quot;, point)\n\ntags = &#x5B;&quot;urgent&quot;]\nrecord = (42, tags)\ntags.append(&quot;review&quot;)\nprint(&quot;mutable member:&quot;, record)\n\nprices = {(42, &quot;usd&quot;): 19.5}\nprint(&quot;tuple key:&quot;, prices&#x5B;(42, &quot;usd&quot;)])\ntry:\n    prices&#x5B;(&#x5B;42], &quot;usd&quot;)] = 19.5\nexcept TypeError as error:\n    print(&quot;list member key:&quot;, type(error).__name__)\n<\/pre><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; notranslate\">\nslot update: TypeError\nreplacement tuple: (99, 7)\nmutable member: (42, &#x5B;&#039;urgent&#039;, &#039;review&#039;])\ntuple key: 19.5\nlist member key: TypeError\n<\/pre><\/div>\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"499\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output.png\" alt=\"AskPython terminal output demonstrating tuple immutability, mutable members and hashable keys\" class=\"wp-image-71980\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output-300x78.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output-1024x266.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output-768x200.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/tuple-edges-output-1536x399.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption>Output from the captured run of python3 tuple_edges.py.<\/figcaption><\/figure>\n\n\n<p class=\"wp-block-paragraph\">The failed assignment leaves the tuple value unchanged, while the second assignment points the name at a new tuple. A list inside the tuple can still change because the tuple holds a reference to it. A tuple serves as a dictionary key only when each member is hashable, so the nested list makes this key invalid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Errors with Python Tuples<\/h2>\n\n\n<p class=\"wp-block-paragraph\">These errors usually come from confusing a tuple&#8217;s fixed shape with its contents, or from assuming each operation returns the same kind of object. Match the symptom to the operation before changing the data structure.<\/p>\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Situation<\/th><th>What happens<\/th><th>What to do<\/th><\/tr><\/thead><tbody>\n<tr><td>Replace, add or remove a tuple position<\/td><td>Item assignment raises TypeError. Tuples have no in-place item insertion or removal.<\/td><td>Create a new tuple, or use a list if the sequence itself must change.<\/td><\/tr>\n<tr><td>Read an index that is outside the tuple<\/td><td>Python raises IndexError.<\/td><td>Check the available length or use a slice when a shorter sequence is valid.<\/td><\/tr>\n<tr><td>Unpack into too few or too many names<\/td><td>Python raises ValueError when the target count does not match.<\/td><td>Match the record shape or use one starred target for a variable middle section.<\/td><\/tr>\n<tr><td>Use a tuple containing a list as a dictionary key<\/td><td>The tuple is unhashable because one of its members is unhashable.<\/td><td>Use hashable members, or choose another key structure that reflects the data.<\/td><\/tr>\n<tr><td>Join a tuple directly to a list<\/td><td>Sequence concatenation fails because the operand types differ.<\/td><td>Convert deliberately so both operands have the same sequence type.<\/td><\/tr>\n<\/tbody><\/table><\/figure>\n\n\n<p class=\"wp-block-paragraph\">A slice or repeated tuple creates a new outer tuple, not a deep copy of the values inside it. If two tuple values refer to one mutable list, a change through that list appears through both tuples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion: Choose a Tuple for a Fixed Record<\/h2>\n\n\n<p class=\"wp-block-paragraph\">Use a tuple when order and field roles are stable, such as a coordinate or a small returned record. Use a list when membership needs to change. Keep the singleton comma, unpack against the record&#8217;s shape, and check member hashability before using a tuple as a key.<\/p>\n\n\n<p class=\"wp-block-paragraph\">For the language rules, start with the Python Tutorial&#8217;s <a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#tuples-and-sequences\" target=\"_blank\" rel=\"noopener noreferrer\">tuples and sequences section<\/a>. The <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#tuples\" target=\"_blank\" rel=\"noopener noreferrer\">built-in types reference<\/a> documents tuple operations, and the <a href=\"https:\/\/docs.python.org\/3\/reference\/datamodel.html#object.__hash__\" target=\"_blank\" rel=\"noopener noreferrer\">data model&#8217;s hash contract<\/a> explains dictionary-key requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n<p class=\"wp-block-paragraph\">These answers cover two small tuple operations and what concatenation changes.<\/p>\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does a tuple&#8217;s count method return?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It returns the number of times a value occurs in the tuple. It does not change the tuple.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How does a tuple&#8217;s index method choose a result?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It returns the first position matching the requested value. Optional start and stop bounds limit the search; if no value matches, Python raises ValueError.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Does concatenating two tuples change either one?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Concatenation creates a new tuple and leaves both original tuples unchanged. It combines their values in order.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sensor records can keep an identifier and reading in a fixed order. A Python tuple groups values in order and keeps its positions fixed after creation. I&#8217;ll show how to create a tuple and explain when its fixed positions suit a record. TL;DR A Python tuple is an ordered sequence whose positions cannot be reassigned [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":71976,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tuple"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/71976"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}