{"id":28357,"date":"2026-02-28T16:48:12","date_gmt":"2026-02-28T16:48:12","guid":{"rendered":"https:\/\/linux.how2shout.com\/?p=28357"},"modified":"2026-02-28T16:48:26","modified_gmt":"2026-02-28T16:48:26","slug":"what-is-a-tuple-in-python","status":"publish","type":"post","link":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/","title":{"rendered":"What Is a Tuple in Python? Syntax, Examples, and When to Use One"},"content":{"rendered":"\n<p>A tuple in Python is an ordered, immutable collection of elements. Once you create a tuple, you cannot change, add, or remove its items. That single characteristic \u2014 immutability \u2014 is what separates tuples from lists and makes them one of the most misunderstood data structures in the language.<\/p>\n\n\n\n<p>If you&#8217;ve worked with <strong>Python <\/strong>lists and wondered why <strong>tuples <\/strong>exist at all, you&#8217;re not alone. Most beginners treat tuples as <strong>&#8220;lists you can&#8217;t edit&#8221;<\/strong> and never look deeper. But tuples serve a <strong>specific purpose in Python&#8217;s design: <\/strong>they represent fixed collections of data where the structure itself carries meaning. Coordinates, database rows, RGB colors, and function return values \u2014 these are all naturally tuple-shaped data.<\/p>\n\n\n\n<p>This article explains what tuples are, how to create and use them, how they differ from lists in practical terms, and when choosing a tuple over a list actually matters for your code&#8217;s performance and reliability.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Answer: What Is a Tuple in Python?<\/h2>\n\n\n\n<p>A tuple is a built-in Python data type that stores an ordered sequence of elements inside parentheses <code>()<\/code>. Tuples are immutable (cannot be modified after creation), ordered (maintain insertion order), and allow duplicate values. They can hold elements of any data type, including other tuples, lists, and mixed types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Creating a tuple\ncoordinates = (40.7128, -74.0060)\nperson = (\"Alice\", 30, \"Engineer\")\nsingle = (42,)     # Note the trailing comma for single-element tuples\nempty = ()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create a Tuple in Python<\/h2>\n\n\n\n<p>There are four ways to create a tuple, and each has a specific use case.<\/p>\n\n\n\n<p><strong>1. Using parentheses (most common)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">fruits = (\"apple\", \"banana\", \"cherry\")\nprint(fruits)   # Output: ('apple', 'banana', 'cherry')<\/code><\/pre>\n\n\n\n<p><strong>2. Without parentheses (tuple packing)<\/strong><\/p>\n\n\n\n<p>Python treats any comma-separated sequence of values as a tuple, even without parentheses. This is called tuple packing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">colors = \"red\", \"green\", \"blue\"\nprint(type(colors))   # Output: &lt;class 'tuple'&gt;<\/code><\/pre>\n\n\n\n<p>This works because it&#8217;s the commas that define a tuple, not the parentheses. The parentheses are just there for readability.<\/p>\n\n\n\n<p><strong>3. Using the tuple() constructor<\/strong><\/p>\n\n\n\n<p>You can convert any iterable \u2014 a list, string, set, or range \u2014 into a tuple using the <code>tuple()<\/code> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">from_list = tuple([1, 2, 3])\nfrom_string = tuple(\"hello\")\nfrom_range = tuple(range(5))\n\nprint(from_list)     # Output: (1, 2, 3)\nprint(from_string)   # Output: ('h', 'e', 'l', 'l', 'o')\nprint(from_range)    # Output: (0, 1, 2, 3, 4)<\/code><\/pre>\n\n\n\n<p><strong>4. Single-element tuple (watch the comma)<\/strong><\/p>\n\n\n\n<p>This is one of the most common mistakes in Python. A single value inside parentheses is not a tuple \u2014 it&#8217;s just that value. You must include a trailing comma.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">not_a_tuple = (\"hello\")\nis_a_tuple = (\"hello\",)\n\nprint(type(not_a_tuple))   # Output: &lt;class 'str'&gt;\nprint(type(is_a_tuple))    # Output: &lt;class 'tuple'&gt;<\/code><\/pre>\n\n\n\n<p>Forgetting the trailing comma is a bug that won&#8217;t raise an error \u2014 Python silently treats it as a string (or integer, or whatever type the value is). This is a subtle issue that trips up even experienced developers.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Tuple Elements<\/h2>\n\n\n\n<p>Tuples support the same indexing and slicing operations as lists and strings.<\/p>\n\n\n\n<p><strong>Indexing (starts at 0)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">languages = (\"Python\", \"Java\", \"C++\", \"Rust\")\n\nprint(languages[0])    # Output: Python\nprint(languages[2])    # Output: C++\nprint(languages[-1])   # Output: Rust (last element)\nprint(languages[-2])   # Output: C++ (second to last)<\/code><\/pre>\n\n\n\n<p><strong>Slicing<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n\nprint(numbers[2:5])     # Output: (2, 3, 4)\nprint(numbers[:3])      # Output: (0, 1, 2)\nprint(numbers[7:])      # Output: (7, 8, 9)\nprint(numbers[::2])     # Output: (0, 2, 4, 6, 8)\nprint(numbers[::-1])    # Output: (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)<\/code><\/pre>\n\n\n\n<p><strong>Nested tuple access<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9))\n\nprint(matrix[1])        # Output: (4, 5, 6)\nprint(matrix[1][2])     # Output: 6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple Unpacking<\/h2>\n\n\n\n<p>Unpacking is one of the most useful features of tuples and something Python developers use constantly in real code.<\/p>\n\n\n\n<p><strong>Basic unpacking<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">person = (\"Alice\", 30, \"Engineer\")\nname, age, role = person\n\nprint(name)    # Output: Alice\nprint(age)     # Output: 30\nprint(role)    # Output: Engineer<\/code><\/pre>\n\n\n\n<p>The number of variables on the left must match the number of elements in the tuple. If they don&#8217;t, Python raises a <code>ValueError<\/code>.<\/p>\n\n\n\n<p><strong>Extended unpacking with the * operator<\/strong><\/p>\n\n\n\n<p>When you don&#8217;t know the exact length or only need the first and last elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">scores = (95, 88, 76, 91, 84, 72)\nfirst, *middle, last = scores\n\nprint(first)     # Output: 95\nprint(middle)    # Output: [88, 76, 91, 84]\nprint(last)      # Output: 72<\/code><\/pre>\n\n\n\n<p>The <code>*<\/code> variable collects all remaining elements into a list. This is particularly useful when processing data rows where you care about specific positions but want to capture everything else.<\/p>\n\n\n\n<p><strong>Swapping variables<\/strong><\/p>\n\n\n\n<p>Tuple unpacking enables Python&#8217;s elegant variable swap syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">a, b = 1, 2\na, b = b, a\nprint(a, b)    # Output: 2 1<\/code><\/pre>\n\n\n\n<p>No temporary variable needed. Under the hood, Python creates a tuple <code>(b, a)<\/code> and unpacks it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why Tuples Are Immutable (And Why That Matters)<\/h2>\n\n\n\n<p>Immutability means you cannot change a tuple after it&#8217;s created. Any attempt to modify, add, or remove elements raises a <code>TypeError<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">colors = (\"red\", \"green\", \"blue\")\ncolors[0] = \"yellow\"\n# TypeError: 'tuple' object does not support item assignment<\/code><\/pre>\n\n\n\n<p>You also cannot use <code>append()<\/code>, <code>remove()<\/code>, <code>insert()<\/code>, or any method that modifies the collection. Tuples only have two built-in methods: <code>count()<\/code> and <code>index()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">numbers = (1, 3, 7, 3, 9, 3, 2)\n\nprint(numbers.count(3))    # Output: 3\nprint(numbers.index(7))    # Output: 2 (first occurrence)<\/code><\/pre>\n\n\n\n<p><strong>Why does immutability matter?<\/strong><\/p>\n\n\n\n<p>It serves three practical purposes.<\/p>\n\n\n\n<p>First, tuples are hashable (as long as all their elements are also hashable), which means you can use them as dictionary keys and set members. Lists cannot do this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Tuple as dictionary key \u2014 works\nlocations = {}\nlocations[(40.71, -74.00)] = \"New York\"\nlocations[(51.50, -0.12)] = \"London\"\n\n# List as dictionary key \u2014 fails\nlocations[[40.71, -74.00]] = \"New York\"\n# TypeError: unhashable type: 'list'<\/code><\/pre>\n\n\n\n<p>Second, immutability guarantees data integrity. When you pass a tuple to a function, you know the function cannot accidentally modify your original data. This is important in larger codebases where data flows through many functions.<\/p>\n\n\n\n<p>Third, tuples use less memory and are faster to create than lists. Python can make internal optimizations because it knows the size and contents will never change.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tuple vs List: The Real Differences<\/h2>\n\n\n\n<p>The &#8220;tuples are immutable lists&#8221; explanation is technically accurate but misses the practical picture. Here&#8217;s when each data type is the right choice.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Tuple<\/th><th>List<\/th><\/tr><\/thead><tbody><tr><td>Syntax<\/td><td><code>()<\/code> parentheses<\/td><td><code>[]<\/code> square brackets<\/td><\/tr><tr><td>Mutable<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Hashable<\/td><td>Yes (if elements are hashable)<\/td><td>No<\/td><\/tr><tr><td>Methods available<\/td><td>2 (<code>count<\/code>, <code>index<\/code>)<\/td><td>11+ (<code>append<\/code>, <code>sort<\/code>, <code>remove<\/code>, etc.)<\/td><\/tr><tr><td>Memory usage<\/td><td>Lower<\/td><td>Higher (over-allocates for growth)<\/td><\/tr><tr><td>Creation speed<\/td><td>Faster<\/td><td>Slower<\/td><\/tr><tr><td>Use as dict key<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Iteration speed<\/td><td>Slightly faster<\/td><td>Slightly slower<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Memory difference in practice:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import sys\n\nmy_list = [1, 2, 3, 4, 5]\nmy_tuple = (1, 2, 3, 4, 5)\n\nprint(sys.getsizeof(my_list))    # Output: 104 bytes\nprint(sys.getsizeof(my_tuple))   # Output: 80 bytes<\/code><\/pre>\n\n\n\n<p>Lists over-allocate memory to allow for future growth (like <code>append()<\/code>). Tuples allocate exactly the memory needed, nothing more. For a single tuple, 24 bytes is negligible. But if you&#8217;re storing millions of records \u2014 say, loading rows from a database \u2014 the memory difference adds up.<\/p>\n\n\n\n<p><strong>Creation speed difference:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import timeit\n\nlist_time = timeit.timeit(\"[1, 2, 3, 4, 5]\", number=1_000_000)\ntuple_time = timeit.timeit(\"(1, 2, 3, 4, 5)\", number=1_000_000)\n\nprint(f\"List:  {list_time:.4f}s\")    # ~0.05s\nprint(f\"Tuple: {tuple_time:.4f}s\")   # ~0.006s<\/code><\/pre>\n\n\n\n<p>Tuple creation is roughly 5-10x faster for small collections. This happens because Python caches small tuples and reuses them, while lists must always allocate fresh memory.<\/p>\n\n\n\n<p><strong>When to use a tuple:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Fixed data that shouldn&#8217;t change (coordinates, database records, configuration values)<\/li>\n\n\n\n<li>Dictionary keys or set members that need compound values<\/li>\n\n\n\n<li>Function return values (Python functions returning multiple values use tuples by default)<\/li>\n\n\n\n<li>Data you want to protect from accidental modification<\/li>\n<\/ul>\n\n\n\n<p><strong>When to use a list:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Collections that grow, shrink, or get reordered<\/li>\n\n\n\n<li>Data that needs sorting in place<\/li>\n\n\n\n<li>Stacks, queues, or any structure that requires <code>append()<\/code> and <code>pop()<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Common Tuple Operations<\/h2>\n\n\n\n<p><strong>Concatenation and repetition<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">tuple_a = (1, 2, 3)\ntuple_b = (4, 5, 6)\n\ncombined = tuple_a + tuple_b\nprint(combined)    # Output: (1, 2, 3, 4, 5, 6)\n\nrepeated = tuple_a * 3\nprint(repeated)    # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)<\/code><\/pre>\n\n\n\n<p>Note that concatenation and repetition create new tuples \u2014 they don&#8217;t modify the originals.<\/p>\n\n\n\n<p><strong>Membership testing<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">colors = (\"red\", \"green\", \"blue\")\n\nprint(\"red\" in colors)       # Output: True\nprint(\"yellow\" in colors)    # Output: False\nprint(\"yellow\" not in colors)  # Output: True<\/code><\/pre>\n\n\n\n<p><strong>Length, min, max, sum<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">numbers = (15, 8, 42, 23, 4)\n\nprint(len(numbers))    # Output: 5\nprint(min(numbers))    # Output: 4\nprint(max(numbers))    # Output: 42\nprint(sum(numbers))    # Output: 92<\/code><\/pre>\n\n\n\n<p><strong>Iterating through a tuple<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">fruits = (\"apple\", \"banana\", \"cherry\")\n\nfor fruit in fruits:\n    print(fruit)\n\n# With index using enumerate\nfor index, fruit in enumerate(fruits):\n    print(f\"{index}: {fruit}\")<\/code><\/pre>\n\n\n\n<p><strong>Sorting a tuple (creates a new list)<\/strong><\/p>\n\n\n\n<p>Since tuples are immutable, you cannot sort them in place. The <code>sorted()<\/code> function returns a new sorted list, which you can convert back to a tuple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">numbers = (42, 15, 8, 23, 4)\nsorted_numbers = tuple(sorted(numbers))\nprint(sorted_numbers)    # Output: (4, 8, 15, 23, 42)<\/code><\/pre>\n\n\n\n<p><strong>Deleting a tuple<\/strong><\/p>\n\n\n\n<p>You cannot delete individual elements from a tuple, but you can delete the entire tuple using <code>del<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">temp = (1, 2, 3)\ndel temp\n# print(temp) would raise NameError: name 'temp' is not defined<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Workaround: How to &#8220;Modify&#8221; a Tuple<\/h2>\n\n\n\n<p>Since tuples are immutable, the standard workaround is to convert the tuple to a list, make your changes, and convert it back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">original = (\"apple\", \"banana\", \"cherry\")\n\n# Convert to list, modify, convert back\ntemp_list = list(original)\ntemp_list[1] = \"mango\"\nmodified = tuple(temp_list)\n\nprint(modified)    # Output: ('apple', 'mango', 'cherry')<\/code><\/pre>\n\n\n\n<p>This creates a new tuple \u2014 the original remains unchanged. If you find yourself doing this frequently, it&#8217;s a sign you should be using a list instead of a tuple in the first place.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Use Cases for Tuples<\/h2>\n\n\n\n<p>Tuples aren&#8217;t just a theoretical concept. Here are practical situations where they are the correct choice over lists.<\/p>\n\n\n\n<p><strong>Returning multiple values from a function<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">def get_user():\n    return \"Alice\", 30, \"alice@example.com\"\n\nname, age, email = get_user()<\/code><\/pre>\n\n\n\n<p>Python functions that return multiple values return them as a tuple by default. The return statement <code>return a, b, c<\/code> is equivalent to <code>return (a, b, c)<\/code>.<\/p>\n\n\n\n<p><strong>Using tuples as dictionary keys<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Storing data by (latitude, longitude) coordinates\nweather_data = {\n    (28.6139, 77.2090): \"Delhi - 35\u00b0C\",\n    (19.0760, 72.8777): \"Mumbai - 30\u00b0C\",\n    (13.0827, 80.2707): \"Chennai - 32\u00b0C\",\n}\n\nprint(weather_data[(28.6139, 77.2090)])    # Output: Delhi - 35\u00b0C<\/code><\/pre>\n\n\n\n<p>This works because tuples are hashable. You cannot do this with lists.<\/p>\n\n\n\n<p><strong>Storing records from a database<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Each row is a tuple \u2014 fixed structure, shouldn't change\nemployees = [\n    (101, \"Alice\", \"Engineering\", 95000),\n    (102, \"Bob\", \"Marketing\", 78000),\n    (103, \"Carol\", \"Engineering\", 102000),\n]\n\nfor emp_id, name, dept, salary in employees:\n    print(f\"{name} works in {dept}\")<\/code><\/pre>\n\n\n\n<p><strong>Named tuples for cleaner code<\/strong><\/p>\n\n\n\n<p>When tuples get complex, accessing elements by index becomes hard to read. Python&#8217;s <code>namedtuple<\/code> from the <code>collections<\/code> module solves this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">from collections import namedtuple\n\nPoint = namedtuple(\"Point\", [\"x\", \"y\"])\np = Point(11, 22)\n\nprint(p.x)    # Output: 11\nprint(p.y)    # Output: 22\nprint(p[0])   # Output: 11 (still supports indexing)<\/code><\/pre>\n\n\n\n<p>Named tuples give you the readability of a class with the memory efficiency and immutability of a tuple.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1772292208811\"><strong class=\"schema-faq-question\"><strong>Q: What is a tuple in Python, in simple words?<\/strong><\/strong> <p class=\"schema-faq-answer\">A: A tuple is an ordered collection of values that cannot be changed after creation. Think of it as a read-only list. You write it with parentheses \u2014 <code>(1, 2, 3)<\/code> \u2014 and you can store any type of data inside it, including numbers, strings, other tuples, and mixed types.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1772292218826\"><strong class=\"schema-faq-question\"><strong>Q: Can a tuple contain a list?<\/strong><\/strong> <p class=\"schema-faq-answer\">A: Yes. A tuple can contain any data type as an element, including lists. The tuple itself remains immutable (you can&#8217;t swap out the list for something else), but the list inside it can still be modified. However, a tuple containing a mutable element like a list is no longer hashable and cannot be used as a dictionary key.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1772292278403\"><strong class=\"schema-faq-question\"><strong>Q: When should I use a tuple instead of a list?<\/strong><\/strong> <p class=\"schema-faq-answer\">A: Use a tuple when your data is fixed and shouldn&#8217;t change: coordinates, database records, configuration values, function return values, and dictionary keys. Use a list when you need to add, remove, sort, or change elements. If you&#8217;re unsure, ask yourself: &#8220;Will this collection ever need to be modified?&#8221; If no, use a tuple.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1772292285097\"><strong class=\"schema-faq-question\"><strong>Q: Are tuples faster than lists in Python?<\/strong><\/strong> <p class=\"schema-faq-answer\">A: Yes, for creation and iteration. Tuple creation is roughly 5-10x faster than list creation for small collections because Python caches and reuses small tuples. Tuples also use about 15-20% less memory than equivalent lists. For element access (reading a value by index), the difference is negligible. The speed advantage matters most when you&#8217;re creating millions of small collections, like processing database rows or CSV records.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1772292298000\"><strong class=\"schema-faq-question\"><strong>Q: Why do I get TypeError: &#8216;tuple&#8217; object does not support item assignment?<\/strong><\/strong> <p class=\"schema-faq-answer\">A: This error occurs when you try to change an element of a tuple using index assignment, like <code>my_tuple[0] = \"new_value\"<\/code>. Tuples are immutable \u2014 they cannot be modified after creation. If you need to change the data, either convert to a list first or create a new tuple with the desired values.<\/p> <\/div> <\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>A tuple in Python is an ordered, immutable sequence that holds any type of data. You create tuples with parentheses <code>()<\/code> or just commas, access elements with indexing and slicing, and unpack them into individual variables. The two key things that separate tuples from lists are immutability (you can&#8217;t change them) and hashability (you can use them as dictionary keys).<\/p>\n\n\n\n<p>In practice, use tuples for fixed-structure data like coordinates, database records, and function return values. Use lists when your collection needs to grow, shrink, or change. And if you&#8217;re working with large datasets where memory or creation speed matters, tuples offer a measurable performance advantage over lists.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A tuple in Python is an ordered, immutable collection of elements. Once you create a tuple, you cannot change, add, or remove its items. That single characteristic \u2014 immutability \u2014 is what separates tuples from lists and makes them one of the most misunderstood data structures in the language. If you&#8217;ve worked with Python lists [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":28360,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","_mbp_gutenberg_autopost":false,"footnotes":""},"categories":[4],"tags":[2932,2931,2923],"class_list":{"0":"post-28357","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-linux","8":"tag-coding","9":"tag-developer","10":"tag-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What Is a Tuple in Python? Syntax, Examples, and When to Use One - LinuxShout<\/title>\n<meta name=\"description\" content=\"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What Is a Tuple in Python? Syntax, Examples, and When to Use One\" \/>\n<meta property=\"og:description\" content=\"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxShout\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/how2shout\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-28T16:48:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-28T16:48:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1535\" \/>\n\t<meta property=\"og:image:height\" content=\"873\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Heyan Maurya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@h2smedia\" \/>\n<meta name=\"twitter:site\" content=\"@h2smedia\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Heyan Maurya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/\"},\"author\":{\"name\":\"Heyan Maurya\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#\\\/schema\\\/person\\\/102d73e20384ea409022d5bafddc0f72\"},\"headline\":\"What Is a Tuple in Python? Syntax, Examples, and When to Use One\",\"datePublished\":\"2026-02-28T16:48:12+00:00\",\"dateModified\":\"2026-02-28T16:48:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/\"},\"wordCount\":1592,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/what-is-a-tuple-in-python.webp\",\"keywords\":[\"coding\",\"developer\",\"python\"],\"articleSection\":[\"Linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#respond\"]}],\"copyrightYear\":\"2026\",\"copyrightHolder\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#organization\"}},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/\",\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/\",\"name\":\"What Is a Tuple in Python? Syntax, Examples, and When to Use One - LinuxShout\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/what-is-a-tuple-in-python.webp\",\"datePublished\":\"2026-02-28T16:48:12+00:00\",\"dateModified\":\"2026-02-28T16:48:26+00:00\",\"description\":\"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292208811\"},{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292218826\"},{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292278403\"},{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292285097\"},{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292298000\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/what-is-a-tuple-in-python.webp\",\"contentUrl\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/what-is-a-tuple-in-python.webp\",\"width\":1535,\"height\":873,\"caption\":\"what is a tuple in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linux.how2shout.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What Is a Tuple in Python? Syntax, Examples, and When to Use One\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#website\",\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/\",\"name\":\"LinuxShout\",\"description\":\"Find the open source solutions\",\"publisher\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#organization\"},\"alternateName\":\"Linux how2shout\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linux.how2shout.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#organization\",\"name\":\"LinuxShout\",\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Linux-Shout-Logo.png\",\"contentUrl\":\"https:\\\/\\\/linux.how2shout.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Linux-Shout-Logo.png\",\"width\":503,\"height\":349,\"caption\":\"LinuxShout\"},\"image\":{\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/how2shout\",\"https:\\\/\\\/x.com\\\/h2smedia\",\"https:\\\/\\\/instagram.com\\\/h2smedia\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/h2smedia\\\/\",\"https:\\\/\\\/www.pinterest.com\\\/how2shout\",\"https:\\\/\\\/youtube.com\\\/h2smedia\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/#\\\/schema\\\/person\\\/102d73e20384ea409022d5bafddc0f72\",\"name\":\"Heyan Maurya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g\",\"caption\":\"Heyan Maurya\"},\"description\":\"I have a keen interest in all kinds of technologies, from consumer-tech to enterprise solutions. However, I am still trying to learn Linux and whatever the problems I face, trying to give solutions for the same, here...\",\"sameAs\":[\"https:\\\/\\\/www.how2shout.com\\\/\"],\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/author\\\/heyan\\\/\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292208811\",\"position\":1,\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292208811\",\"name\":\"Q: What is a tuple in Python, in simple words?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A: A tuple is an ordered collection of values that cannot be changed after creation. Think of it as a read-only list. You write it with parentheses \u2014 (1, 2, 3) \u2014 and you can store any type of data inside it, including numbers, strings, other tuples, and mixed types.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292218826\",\"position\":2,\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292218826\",\"name\":\"Q: Can a tuple contain a list?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A: Yes. A tuple can contain any data type as an element, including lists. The tuple itself remains immutable (you can't swap out the list for something else), but the list inside it can still be modified. However, a tuple containing a mutable element like a list is no longer hashable and cannot be used as a dictionary key.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292278403\",\"position\":3,\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292278403\",\"name\":\"Q: When should I use a tuple instead of a list?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A: Use a tuple when your data is fixed and shouldn't change: coordinates, database records, configuration values, function return values, and dictionary keys. Use a list when you need to add, remove, sort, or change elements. If you're unsure, ask yourself: \\\"Will this collection ever need to be modified?\\\" If no, use a tuple.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292285097\",\"position\":4,\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292285097\",\"name\":\"Q: Are tuples faster than lists in Python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A: Yes, for creation and iteration. Tuple creation is roughly 5-10x faster than list creation for small collections because Python caches and reuses small tuples. Tuples also use about 15-20% less memory than equivalent lists. For element access (reading a value by index), the difference is negligible. The speed advantage matters most when you're creating millions of small collections, like processing database rows or CSV records.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292298000\",\"position\":5,\"url\":\"https:\\\/\\\/linux.how2shout.com\\\/what-is-a-tuple-in-python\\\/#faq-question-1772292298000\",\"name\":\"Q: Why do I get TypeError: 'tuple' object does not support item assignment?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A: This error occurs when you try to change an element of a tuple using index assignment, like my_tuple[0] = \\\"new_value\\\". Tuples are immutable \u2014 they cannot be modified after creation. If you need to change the data, either convert to a list first or create a new tuple with the desired values.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What Is a Tuple in Python? Syntax, Examples, and When to Use One - LinuxShout","description":"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What Is a Tuple in Python? Syntax, Examples, and When to Use One","og_description":"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.","og_url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/","og_site_name":"LinuxShout","article_publisher":"https:\/\/www.facebook.com\/how2shout","article_published_time":"2026-02-28T16:48:12+00:00","article_modified_time":"2026-02-28T16:48:26+00:00","og_image":[{"width":1535,"height":873,"url":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp","type":"image\/webp"}],"author":"Heyan Maurya","twitter_card":"summary_large_image","twitter_creator":"@h2smedia","twitter_site":"@h2smedia","twitter_misc":{"Written by":"Heyan Maurya","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#article","isPartOf":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/"},"author":{"name":"Heyan Maurya","@id":"https:\/\/linux.how2shout.com\/#\/schema\/person\/102d73e20384ea409022d5bafddc0f72"},"headline":"What Is a Tuple in Python? Syntax, Examples, and When to Use One","datePublished":"2026-02-28T16:48:12+00:00","dateModified":"2026-02-28T16:48:26+00:00","mainEntityOfPage":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/"},"wordCount":1592,"commentCount":0,"publisher":{"@id":"https:\/\/linux.how2shout.com\/#organization"},"image":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp","keywords":["coding","developer","python"],"articleSection":["Linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#respond"]}],"copyrightYear":"2026","copyrightHolder":{"@id":"https:\/\/linux.how2shout.com\/#organization"}},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/","url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/","name":"What Is a Tuple in Python? Syntax, Examples, and When to Use One - LinuxShout","isPartOf":{"@id":"https:\/\/linux.how2shout.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#primaryimage"},"image":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp","datePublished":"2026-02-28T16:48:12+00:00","dateModified":"2026-02-28T16:48:26+00:00","description":"A tuple in Python is an immutable, ordered collection created with parentheses. Learn tuple syntax, indexing, unpacking, tuple vs list differences, and real-world use cases with code examples.","breadcrumb":{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292208811"},{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292218826"},{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292278403"},{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292285097"},{"@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292298000"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#primaryimage","url":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp","contentUrl":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2026\/02\/what-is-a-tuple-in-python.webp","width":1535,"height":873,"caption":"what is a tuple in python"},{"@type":"BreadcrumbList","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linux.how2shout.com\/"},{"@type":"ListItem","position":2,"name":"What Is a Tuple in Python? Syntax, Examples, and When to Use One"}]},{"@type":"WebSite","@id":"https:\/\/linux.how2shout.com\/#website","url":"https:\/\/linux.how2shout.com\/","name":"LinuxShout","description":"Find the open source solutions","publisher":{"@id":"https:\/\/linux.how2shout.com\/#organization"},"alternateName":"Linux how2shout","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linux.how2shout.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linux.how2shout.com\/#organization","name":"LinuxShout","url":"https:\/\/linux.how2shout.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linux.how2shout.com\/#\/schema\/logo\/image\/","url":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2020\/06\/Linux-Shout-Logo.png","contentUrl":"https:\/\/linux.how2shout.com\/wp-content\/uploads\/2020\/06\/Linux-Shout-Logo.png","width":503,"height":349,"caption":"LinuxShout"},"image":{"@id":"https:\/\/linux.how2shout.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/how2shout","https:\/\/x.com\/h2smedia","https:\/\/instagram.com\/h2smedia","https:\/\/www.linkedin.com\/company\/h2smedia\/","https:\/\/www.pinterest.com\/how2shout","https:\/\/youtube.com\/h2smedia"]},{"@type":"Person","@id":"https:\/\/linux.how2shout.com\/#\/schema\/person\/102d73e20384ea409022d5bafddc0f72","name":"Heyan Maurya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b07b375c19891b0589da722cb1e3dff333ec63dd8467afc114611044e87cfe9a?s=96&d=mm&r=g","caption":"Heyan Maurya"},"description":"I have a keen interest in all kinds of technologies, from consumer-tech to enterprise solutions. However, I am still trying to learn Linux and whatever the problems I face, trying to give solutions for the same, here...","sameAs":["https:\/\/www.how2shout.com\/"],"url":"https:\/\/linux.how2shout.com\/author\/heyan\/"},{"@type":"Question","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292208811","position":1,"url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292208811","name":"Q: What is a tuple in Python, in simple words?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A: A tuple is an ordered collection of values that cannot be changed after creation. Think of it as a read-only list. You write it with parentheses \u2014 (1, 2, 3) \u2014 and you can store any type of data inside it, including numbers, strings, other tuples, and mixed types.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292218826","position":2,"url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292218826","name":"Q: Can a tuple contain a list?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A: Yes. A tuple can contain any data type as an element, including lists. The tuple itself remains immutable (you can't swap out the list for something else), but the list inside it can still be modified. However, a tuple containing a mutable element like a list is no longer hashable and cannot be used as a dictionary key.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292278403","position":3,"url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292278403","name":"Q: When should I use a tuple instead of a list?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A: Use a tuple when your data is fixed and shouldn't change: coordinates, database records, configuration values, function return values, and dictionary keys. Use a list when you need to add, remove, sort, or change elements. If you're unsure, ask yourself: \"Will this collection ever need to be modified?\" If no, use a tuple.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292285097","position":4,"url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292285097","name":"Q: Are tuples faster than lists in Python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A: Yes, for creation and iteration. Tuple creation is roughly 5-10x faster than list creation for small collections because Python caches and reuses small tuples. Tuples also use about 15-20% less memory than equivalent lists. For element access (reading a value by index), the difference is negligible. The speed advantage matters most when you're creating millions of small collections, like processing database rows or CSV records.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292298000","position":5,"url":"https:\/\/linux.how2shout.com\/what-is-a-tuple-in-python\/#faq-question-1772292298000","name":"Q: Why do I get TypeError: 'tuple' object does not support item assignment?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"A: This error occurs when you try to change an element of a tuple using index assignment, like my_tuple[0] = \"new_value\". Tuples are immutable \u2014 they cannot be modified after creation. If you need to change the data, either convert to a list first or create a new tuple with the desired values.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/posts\/28357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/comments?post=28357"}],"version-history":[{"count":3,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/posts\/28357\/revisions"}],"predecessor-version":[{"id":28363,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/posts\/28357\/revisions\/28363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/media\/28360"}],"wp:attachment":[{"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/media?parent=28357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/categories?post=28357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linux.how2shout.com\/wp-json\/wp\/v2\/tags?post=28357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}