Python Tuple

Beginner
8 min read
Updated: Nov 2026
3 examples
Immutable sequences

Definition and Usage

A tuple is an ordered group of values, like a list, but it is immutable: once you create it, its items cannot be added, removed, or replaced. You write it with commas, usually inside parentheses: (3, 4). Each item has a position, called an index, starting at 0.

Use a tuple for values that belong together and should stay fixed, such as a point, a date, or a row of data. A tuple has only two methods, count() and index(), listed on the Tuple Methods index.

Python Syntax

Commas create the tuple; square brackets after it read one item by index or several by slice:

Python
point = (3, 4)                         # parentheses and commas
colors = "red", "green", "blue"        # the commas make the tuple
print(point[0], colors[-1])            # 3 blue
print(colors[:2])                      # ('red', 'green')
print(len(colors), "green" in colors)  # 3 True
x, y = point                           # unpacking
print(x + y)                           # 7
The comma makes the tuple, not the parentheses.

(5) is just the number 5; (5,) is a tuple holding 5. The only tuple you write with parentheses alone is the empty one, ().

How Tuples Behave

RuleWhat it means
OrderedItems keep the position you gave them, and two tuples are equal only when they hold equal items in the same order.
ImmutableAfter a tuple is created you cannot add, remove, or replace items: t[0] = 1 raises TypeError. "Immutable" simply means "cannot be changed".
Duplicates allowed(6, 6, 6) is a valid tuple of three items, and count() tells you how often a value repeats.
Mixed types("Ada", 36, True, None) holds a str, an int, a bool, and None side by side.
Zero-based indexesThe first item is t[0], and reading a position past the end raises IndexError.
Negative indexest[-1] is the last item and t[-2] the one before it.
HashableA tuple whose items are all hashable (numbers, strings, other such tuples) can be a dict key or a set member. Hashable means Python can turn the value into a fixed number for fast lookups, which only works for values that never change.

Creating Tuples

CodeWhat you get
()Empty tuple; tuple() gives the same.
(7,)One item: the trailing comma is required.
1, 2, 3Tuple packing: commas without parentheses.
tuple([1, 2])From a list, or any other iterable.
tuple("abc")From a string: one character per item.
tuple(range(3))From a range: (0, 1, 2).

The tuple() constructor turns any iterable into a tuple, and len() counts its items. Writing values separated by commas without brackets is called tuple packing.

Immutability: What Can and Cannot Change

Assigning to an item, as in t[0] = 1, raises TypeError, and tuples have no append() or remove(). To get a "changed" tuple, build a new one, such as t + (4,), or convert with list(t), change the list, and turn it back with tuple().

A tuple fixes which objects it holds, not what happens inside them. If one item is a list, that list can still grow: ("box", [1, 2]) becomes ("box", [1, 2, 3]) after an append(3) on the inner list. Such a tuple is also no longer hashable, so it cannot be a dict key.

Packing and Unpacking

CodeWhat it does
a, b = tOne name per item; a different number of items raises ValueError.
a, b = b, aSwap two variables in one line.
first, *rest = tfirst gets item 0 and rest a list of the others.
return x, yA function returns two values packed into one tuple.
for x, y in points:Unpack each (x, y) pair while looping.

Unpacking also works in loops over zip() and enumerate(), which both produce tuples.

Tuple Operators

CodeResult
len(t)Number of items.
x in tTrue if some item is equal to x.
t + otherNew tuple joining both; other must also be a tuple.
t * nNew tuple with the items repeated n times.
(1, 2) < (1, 3)True: tuples compare item by item from the left; the first items are equal, so 2 < 3 decides.
t == otherTrue when both have the same length and equal items in the same order.

Named tuples. When each position has a meaning, collections.namedtuple lets you name the positions, so you can write p.x instead of p[0]. A named tuple is still a tuple: it is immutable and has the same count() and index() methods.

Tuple vs List

FeatureTupleList
Written as(1, 2, 1) or 1, 2, 1[1, 2, 1]
Change after creation?No: immutableYes: mutable
Named methods2: count(), index()11, such as append(), pop(), sort()
Dict key or set member?Yes, if every item is hashableNever
Typical useFixed records: a point, an RGB color, a date as (2026, 11, 19), several return valuesCollections that grow, shrink, or get sorted

Pick a tuple when the data should not change by accident, and a list when it must change. Read more on the Python List page and the List Methods index.

Common Mistakes

MistakeDo this instead
x = (5)That is the int 5. Write (5,) or 5, for a one-item tuple.
t.append(4)Raises AttributeError: tuples have no append(). Build a new tuple with t + (4,).
t.sort()Raises AttributeError too. Use sorted(t), which returns a new list, or tuple(sorted(t)).
a, b = (1, 2, 3)Raises ValueError: three items do not fit two names. Use a, b, c or a, *rest.
{(1, [2]): "x"}Raises TypeError: the list inside makes the tuple unhashable, so it cannot be a dict key.

Python Examples

Fixed data only, so the output is the same on every machine.

Example 1 — Create Tuples Five Ways and Spot the Comma Trap

An empty tuple, a one-item tuple, a number that only looks like a tuple, bare commas, and the tuple() constructor on a list and on a string.

Python
empty = ()
single = (5,)
not_a_tuple = (5)
bare = 1, 2, 3
from_list = tuple([4, 5])
from_text = tuple("abc")

print(empty, single, not_a_tuple)
print(type(single).__name__, type(not_a_tuple).__name__)
print(bare, from_list, from_text)
print(len(empty), len(bare))

How It Works

  1. The comma makes the tuple. (5,) is a tuple with one item, but (5) is just the number 5 inside parentheses, which is why its type name is int.
  2. Parentheses are optional. bare = 1, 2, 3 packs three values into a tuple with commas alone, and Python prints it back with parentheses.
  3. From other sequences. tuple() copies the items of a list, or one character per item from a string; len() reports 0 items for ().

Example 2 — Read an RGB Color and Try to Change It

Read items by position, slice the tuple, try to replace an item, and see that a list stored inside a tuple can still grow.

Python
rgb = (255, 128, 0)
print(rgb[0], rgb[-1])
print(rgb[1:], rgb[::-1])

try:
    rgb[0] = 10
except TypeError:
    print("TypeError: tuples cannot be changed")

shelf = ("box", [1, 2])
shelf[1].append(3)
print(shelf)

How It Works

  1. Reading works like a list. Index 0 is red (255), index -1 counts back from the end to blue (0), and every slice returns a new tuple.
  2. Writing is refused. Assigning to rgb[0] raises TypeError, because a tuple is immutable; the except block prints our own message.
  3. Fixed slots, changeable contents. The tuple still holds the same list object, but that list is mutable, so append(3) changes what you see inside shelf.

Example 3 — Unpack Results, Swap Values, and Use a Tuple as a Dict Key

Return two values from a function, swap two names, catch "the rest" with a star, look up a dict by a coordinate key, and compare two tuples.

Python
def min_max(values):
    return min(values), max(values)


low, high = min_max((7, 3, 9, 4))
print(low, high)

a, b = 1, 2
a, b = b, a
print(a, b)

first, *rest = ("gold", "silver", "bronze")
print(first, rest)

cities = {(40.7, -74.0): "New York"}
print(cities[(40.7, -74.0)], (1, 2) < (1, 3))

How It Works

  1. Several return values. return min(values), max(values) packs two results into one tuple, and low, high = ... unpacks it into two names.
  2. A one-line swap. The right side b, a is packed into a tuple first and then unpacked, so the two names trade values with no temporary variable.
  3. Star, keys, and comparison. *rest collects the leftover items in a list, a tuple of numbers is hashable so it works as a dict key, and tuples compare item by item: 2 is less than 3, so the result is True.

Next: Python Tuple Methods

GroupMethods
Search and countcount(), index()

Frequently Asked Questions

A tuple is a built-in, ordered, immutable sequence written with commas and usually parentheses, such as (3, "a", None). You read items by index, starting at 0, but you cannot change them.
Put a comma after the item: (5,) or just 5,. Without the comma, (5) is only the number 5 inside parentheses.
The tuple itself cannot: you cannot add, remove, or replace its items. But if an item is a mutable object such as a list, that list can still change inside the tuple.
Use a tuple for a fixed group of values, such as a point (x, y), an RGB color, or a record used as a dict key. Use a list when items will be added, removed, or sorted in place.
You build a new tuple: t = t + (4,). For many changes, convert with list(t), change the list, and turn it back with tuple().

Browse the tuple methods

Open the method index for count() and index() guides with examples and FAQs.

Tuple Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful