W3docs

Python Tuples: A Complete Guide

Learn Python tuples from scratch: creation, indexing, slicing, unpacking, methods, and when to choose a tuple over a list.

A tuple is an ordered, immutable sequence in Python. It can hold items of any type — integers, strings, floats, other tuples — and those items keep their position permanently after the tuple is created. This page covers everything you need to work with tuples: how to create them, read from them, combine them, and know when to pick them over a Python list.

What Is a Python Tuple?

A tuple is written as a comma-separated sequence enclosed in parentheses:

python— editable, runs on the server

Three key properties define tuples:

  • Ordered — items have a fixed position (index 0, 1, 2, …).
  • Immutable — you cannot change, add, or remove items after creation.
  • Allows duplicates — the same value can appear more than once.

Tuples vs. Lists

Both tuples and lists store ordered sequences, but their mutability differs fundamentally:

FeatureTupleList
Syntax(1, 2, 3)[1, 2, 3]
MutableNoYes
Use caseFixed data (coordinates, RGB, DB rows)Collections that change over time
PerformanceSlightly faster to iterateSlightly slower

Use a tuple when the data should not change — configuration constants, function return values that carry multiple pieces of related data, or dictionary keys.

Creating Tuples

Basic syntax

List comma-separated values inside parentheses:

python— editable, runs on the server

Mixed types

A tuple can hold items of different types, including other tuples:

mixed = (1, 'hello', 3.14, True)
print(mixed)
# Output: (1, 'hello', 3.14, True)

Empty tuple

empty = ()
print(empty)        # Output: ()
print(type(empty))  # Output: <class 'tuple'>

Single-element tuple

This is a common gotcha. Without the trailing comma, Python treats the parentheses as grouping, not a tuple:

not_a_tuple = (42)
print(type(not_a_tuple))  # Output: <class 'int'>

real_tuple = (42,)
print(type(real_tuple))   # Output: <class 'tuple'>

The trailing comma makes it a tuple — always include it for single-element tuples.

Creating from an iterable

Use the built-in tuple() constructor to convert any iterable into a tuple:

python— editable, runs on the server

Tuple without parentheses (packing)

Python allows you to create a tuple by simply listing values separated by commas — the parentheses are optional:

packed = 1, 2, 3
print(packed)        # Output: (1, 2, 3)
print(type(packed))  # Output: <class 'tuple'>

Accessing Tuple Elements

Positive indexing

Indices start at 0 for the first element:

python— editable, runs on the server

Negative indexing

Negative indices count from the end. -1 is the last element:

python— editable, runs on the server

Slicing

Use [start:stop:step] to extract a sub-tuple. The stop index is excluded:

t = (0, 1, 2, 3, 4)
print(t[1:4])   # Output: (1, 2, 3)
print(t[:3])    # Output: (0, 1, 2)
print(t[2:])    # Output: (2, 3, 4)
print(t[::2])   # Output: (0, 2, 4)

Nested tuples

Access nested elements by chaining index brackets:

nested = ((1, 2), (3, 4), (5, 6))
print(nested[1])     # Output: (3, 4)
print(nested[1][0])  # Output: 3

Checking membership

The in operator tests whether a value exists in a tuple:

fruits = ('apple', 'banana', 'cherry')
print('banana' in fruits)  # Output: True
print('mango' in fruits)   # Output: False

For more indexing and slicing techniques, see Access Tuples.

Tuple Immutability

Once created, a tuple's items cannot be changed:

my_tuple = (1, 2, 3)
my_tuple[0] = 99  # TypeError: 'tuple' object does not support item assignment

If you need a modified version, build a new tuple from slices and concatenation:

python— editable, runs on the server

Note that a tuple can contain mutable objects like lists. The tuple itself is immutable (you cannot swap the list reference), but the list inside can still be modified.

See Update Tuples for a full discussion of workarounds.

Tuple Operations

Concatenation with +

Joining two tuples with + creates a new tuple:

t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2)
# Output: (1, 2, 3, 4)

Repetition with *

Repeat a tuple's contents with *:

t = (0,) * 3
print(t)
# Output: (0, 0, 0)

For all joining strategies, see Join Tuples.

Tuple Methods

Tuples have exactly two built-in methods, because immutability rules out anything that would modify the collection.

count()

Returns the number of times a value appears:

python— editable, runs on the server

index()

Returns the index of the first occurrence of a value. Raises ValueError if the value is not found:

python— editable, runs on the server

See Tuple Methods for a complete reference.

Built-in Functions with Tuples

Python's built-in functions work on any iterable, including tuples:

python— editable, runs on the server

sorted() always returns a list, even when given a tuple as input. Wrap it in tuple() if you need a sorted tuple back.

Tuple Unpacking

Unpacking assigns each element of a tuple to a separate variable in one statement:

python— editable, runs on the server

Extended unpacking with *

A starred variable absorbs any number of remaining elements into a list:

first, *rest = (10, 20, 30, 40)
print(first)  # Output: 10
print(rest)   # Output: [20, 30, 40]

Swapping variables

Tuple unpacking makes variable swapping a one-liner — no temporary variable needed:

x, y = 5, 10
x, y = y, x
print(x, y)
# Output: 10 5

See Unpack Tuples for more patterns including nested unpacking.

Iterating over a Tuple

A for loop visits every element in order:

fruits = ('apple', 'banana', 'cherry')
for fruit in fruits:
    print(fruit)
# Output:
# apple
# banana
# cherry

Use enumerate() when you also need the index:

for i, fruit in enumerate(fruits):
    print(i, fruit)
# Output:
# 0 apple
# 1 banana
# 2 cherry

See Loop Tuples for while loops, zip(), and nested-tuple traversal.

Building Tuples from Comprehensions

Python has no tuple comprehension syntax, but you can pass a generator expression to tuple() to achieve the same result:

python— editable, runs on the server

The expression x**2 for x in my_list inside tuple() is a generator — it produces values one at a time without building an intermediate list.

When to Use Tuples

Tuples shine in specific situations:

  • Multiple return values. Functions can return several values cleanly as a tuple: return x, y.
  • Dictionary keys. Lists cannot be dictionary keys because they are mutable; tuples can.
  • Data integrity. If a collection should not change — RGB values, database row, geographic coordinates — a tuple enforces that at the language level.
  • Slight performance advantage. Tuple iteration and creation are marginally faster than list operations for the same data.
# Using a tuple as a dictionary key
locations = {}
locations[(48.8566, 2.3522)] = 'Paris'
locations[(51.5074, -0.1278)] = 'London'
print(locations[(48.8566, 2.3522)])
# Output: Paris

Practice

Practice
Which of the following statements about Python tuples are correct?
Which of the following statements about Python tuples are correct?
Was this page helpful?