You have been using lists to store everything in Python. But sometimes you need a collection that should never change — coordinates, RGB values, database row IDs. A list will let you modify those freely. Python tuples will not. That difference matters more than most beginners realize, and it shapes how the Python standard library and frameworks use them internally.
In this tutorial, you will learn to:
- Create Python tuples using parentheses and the tuple() constructor
- Access, index, and slice tuple elements safely
- Use tuple methods:
count()andindex() - Understand why tuple immutability matters in real Python code
- Unpack tuples to extract multiple values in a single line
- Compare tuples and lists and know when to use each
Page Contents
What Is a Python Tuple?
A Python tuple is an ordered, immutable collection of items. “Ordered” means items stay in the order you insert them. “Immutable” means once a tuple is created, you cannot change, add, or remove any item in it.
Tuples look nearly identical to lists — the only visible difference is that tuples use parentheses () while lists use square brackets []. But that one syntactic difference signals something important to every Python developer: this data is fixed.
The official Python documentation introduces the tuple as another standard sequence type. Tuples are used throughout Python’s built-in library — dictionary keys, function return values, os.path paths, and more.
Prerequisites
You should know basic Python: variables, basic data types (strings, numbers, booleans), and how to run Python code. If you are new to Python collections, the Python Lists guide is a good starting point — lists and tuples are closely related.
Creating Tuples
Create a tuple by placing items inside parentheses (), separated by commas:
# A tuple of integers
coordinates = (10, 20, 30)
# A tuple of strings
colors = ("red", "green", "blue")
# A mixed-type tuple — perfectly valid
record = ("Alice", 30, True, "engineer")
# Empty tuple
empty = ()
# Single-item tuple — comma is required!
single = ("hello",) # note the trailing comma
not_a_tuple = ("hello") # this is just a string in parentheses
print(coordinates)
print(colors)
print(record)
print(empty)
print(single)
print(type(single)) # <class 'tuple'>
print(type(not_a_tuple)) # <class 'str'>
The trailing comma in a single-item tuple is not optional — it is what makes Python recognise it as a tuple, not just a parenthesised expression.
The tuple() Constructor
You can also create a tuple from any iterable — a list, a string, a range, or a file — using the tuple() constructor:
# From a list
numbers = tuple([1, 2, 3, 4])
print(numbers) # (1, 2, 3, 4)
# From a string — each character becomes an item
letters = tuple("abc")
print(letters) # ('a', 'b', 'c')
# From a range
decades = tuple(range(1990, 2030, 10))
print(decades) # (1990, 2000, 2010, 2020)
# Converting back to a list
back_to_list = list(decades)
print(back_to_list) # [1990, 2000, 2010, 2020]
Accessing Tuple Items
Tuples support the same indexing and slicing syntax as lists. Indexing starts at 0, and negative indexes count from the end:
colors = ("red", "green", "blue", "yellow", "orange")
# Positive indexing
print(colors[0]) # "red" — first item
print(colors[1]) # "green" — second item
print(colors[4]) # "orange" — fifth item
# Negative indexing
print(colors[-1]) # "orange" — last item
print(colors[-2]) # "yellow" — second to last
Warning: Accessing an out-of-range index raises an
IndexError, just like with lists. Always validate the index before accessing it in production code.
Slicing Tuples
Slicing extracts a sub-tuple using [start:stop]. The stop index is excluded — Python slices are half-open intervals:
colors = ("red", "green", "blue", "yellow", "orange")
print(colors[1:4]) # ('green', 'blue', 'yellow') — index 1 to 3
print(colors[:3]) # ('red', 'green', 'blue') — from start to index 2
print(colors[2:]) # ('blue', 'yellow', 'orange') — from index 2 to end
print(colors[-3:]) # ('blue', 'yellow', 'orange') — last 3 items
Tuple Methods
Tuples have exactly two methods — by design. Because tuples are immutable, they do not need the many mutation methods that lists have. The official Python documentation covers these in the sequences section.
count() — How Many Times Does a Value Appear?
values = (1, 2, 3, 2, 1, 2, 4, 2)
print(values.count(1)) # 2
print(values.count(2)) # 4
print(values.count(5)) # 0 — not found
index() — Find the Position of a Value
colors = ("red", "green", "blue", "green", "yellow")
print(colors.index("green")) # 1 — first occurrence
print(colors.index("blue")) # 2
# colors.index("orange") # ValueError — not in tuple
Tuple Immutability — and Why It Matters
The most important property of a tuple is that it is immutable. Once created, you cannot modify, append to, or remove items from a tuple. Attempting to do so raises a TypeError:
point = (10, 20)
# point[0] = 100 # TypeError: 'tuple' object does not support item assignment
# point.append(30) # AttributeError: 'tuple' object has no attribute 'append'
# point.remove(10) # AttributeError: 'tuple' object has no attribute 'remove'
# However, you CAN reassign the variable to a new tuple
point = (100, 200)
print(point) # (100, 200)
But immutability is not a weakness — it is a feature. Immutability means:
- Tuples can be dictionary keys — only immutable types are hashable in Python, and tuples qualify. Lists cannot be dictionary keys.
- Tuples can be used in
setandfrozenset— again, hashability is required. - Function arguments are safer — a function that receives a tuple knows its contents will not change unexpectedly.
- Tuples are slightly faster and use less memory — CPython’s internals optimises immutable objects differently from mutable ones.
Note: Immutability applies to the tuple’s structure, not its contents. If a tuple contains a mutable object like a list, that inner list can still be modified:
mixed = (1, [2, 3], 4)
# The tuple itself cannot be modified:
# mixed[0] = 99 # TypeError
# But the inner list can be modified:
mixed[1].append(99)
print(mixed) # (1, [2, 3, 99], 4)
Tuple Unpacking
Tuple unpacking lets you extract multiple values from a tuple into individual variables in a single line — one of Python’s most loved features:
point = (10, 20, 30)
# Unpack all items into separate variables
x, y, z = point
print(x, y, z) # 10 20 30
# Swap two variables without a temporary — classic Python trick
a, b = 1, 2
a, b = b, a
print(a, b) # 2 1
Use the * operator to capture remaining items when unpacking with a fixed number of variables:
first, *middle, last = (1, 2, 3, 4, 5)
print(first) # 1
print(middle) # [2, 3, 4] — collected as a list
print(last) # 5
# Use * to ignore unwanted items
name, _, age, _ = ("Alice", "red", 30, "blue")
print(name, age) # Alice 30
Real-World Uses of Tuples
Tuples appear throughout Python’s standard library and real-world code for several specific patterns:
Function Return Values
Functions often return multiple values as tuples — Python’s divmod() is a built-in example:
result = divmod(17, 5)
print(result) # (3, 2) — quotient and remainder
quotient, remainder = divmod(17, 5)
print(f"17 divided by 5 is {quotient} remainder {remainder}")
# 17 divided by 5 is 3 remainder 2
# A function returning multiple values
def get_stats(numbers):
return min(numbers), max(numbers), sum(numbers)
low, high, total = get_stats([1, 7, 3, 9, 2])
print(f"Range: {low}-{high}, Sum: {total}") # Range: 1-9, Sum: 22
Dictionary Keys
Because tuples are immutable and hashable, they can serve as dictionary keys — useful for composite keys:
# Composite key: (city, country) → population
population = {
("London", "UK"): 9_000_000,
("Paris", "France"): 2_100_000,
("Tokyo", "Japan"): 13_900_000,
}
print(population[("London", "UK")]) # 9_000_000
# Lists cannot be dictionary keys — this raises TypeError:
# my_dict = {["key"]: "value"} # TypeError: unhashable type 'list'
Named Tuples
The collections.namedtuple factory creates tuple subclasses with named fields — useful for lightweight data records:
from collections import namedtuple
# Define a named tuple type
Point = namedtuple("Point", ["x", "y", "z"])
# Create instances
origin = Point(0, 0, 0)
p1 = Point(10, 20, 30)
# Access by index OR by name
print(p1.x, p1.y, p1.z) # 10 20 30 — named access
print(p1[0], p1[1], p1[2]) # 10 20 30 — index access
# Named tuples are immutable — like regular tuples
# p1.x = 50 # AttributeError
Tuple vs List — When to Use Which
This is the most common question about tuples, and it comes down to one question: should this collection ever change after creation?
| Feature | Tuple | List |
|---|---|---|
| Syntax | (1, 2, 3) | [1, 2, 3] |
| Mutable? | No — immutable | Yes — mutable |
| Dictionary key? | Yes — hashable | No — unhashable |
| set / frozenset? | Yes | No |
| Methods | 2 (count, index) | Many (append, remove, sort, …) |
| Performance | Slightly faster, less memory | Slightly slower, more memory |
| Use when | Fixed data, keys, return values | Collection that will change |
Use a tuple when the data is fixed: coordinates, RGB values, database row IDs, function return values, or any composite dictionary key.
Use a list when you need to add, remove, or modify items — building a shopping cart, collecting user inputs, managing a queue of tasks.
For more on lists and when to use mutable collections, see Python Lists for Beginners.
Common Mistakes and Gotchas
- Forgetting the trailing comma for single-item tuples —
("hello")is a string, not a tuple. Write("hello",)with the comma. - Thinking tuples are completely immutable — if a tuple contains a mutable object (a list or dict), that inner object can still be modified. Only the tuple’s own structure is locked.
- Using a list as a dictionary key —
{["key"]: "value"}raisesTypeError. Use a tuple instead:{("key",): "value"}. - Mutating a list passed into a function — if a function receives a list and you do not want it modified, consider passing a tuple instead. The immutability guarantee is a contract.
- Using
tuple.append()— tuples have noappend()orinsert(). If you need to add items, convert to a list first, or reassign the variable to a new tuple:t = t + (new_item,).
Practice Exercises
Try these exercises on your own before checking the solutions.
Exercise 1: Creating and Accessing Tuples
Create a tuple rgb = ("red", "green", "blue"). Then print:
- The first and last colours
- A slice of just
("green", "blue")
Exercise 2: Tuple Unpacking
A function returns (3, 4). Use tuple unpacking to get width = 3 and height = 4. Then swap their values so width becomes 4 and height becomes 3.
Exercise 3: Tuple Methods
Given values = (1, 2, 3, 2, 4, 2, 5):
- Count how many times
2appears - Find the index of the first
4
Exercise 4: Dictionary with Tuple Keys
Create a dictionary that uses (x, y) coordinates as keys and stores a label string as the value. Add three entries and print one of them.
Exercise 5: Named Tuple
Using collections.namedtuple, create a Person type with fields name, age, and city. Create one instance and print all three fields by name.
Solutions
Solution 1
rgb = ("red", "green", "blue")
print(rgb[0], rgb[-1]) # "red", "blue"
print(rgb[1:]) # ('green', 'blue')
Solution 2
result = (3, 4)
width, height = result
print(width, height) # 3 4
width, height = height, width
print(width, height) # 4 3
Solution 3
values = (1, 2, 3, 2, 4, 2, 5)
print(values.count(2)) # 3
print(values.index(4)) # 4
Solution 4
locations = {
(40.7128, -74.0060): "New York",
(51.5074, -0.1278): "London",
(35.6762, 139.6503): "Tokyo",
}
print(locations[(40.7128, -74.0060)]) # "New York"
Solution 5
from collections import namedtuple
Person = namedtuple("Person", ["name", "age", "city"])
bob = Person("Bob", 28, "Berlin")
print(bob.name, bob.age, bob.city) # Bob 28 Berlin
Summary and Next Steps
You have learned the fundamentals of Python tuples:
- Tuples are ordered and immutable — data is fixed after creation
- Create them with
()— and always include the trailing comma for single items - Access and slice them exactly like lists using
[] - Only two methods:
count()andindex() - Immutability makes tuples hashable — usable as dictionary keys and in sets
- Tuple unpacking is a powerful way to extract multiple values in one line
- Use tuples for fixed data; use lists when you need to mutate
Now that you understand tuples, move on to Python Dictionary — the other essential built-in collection type in Python. Dictionaries and tuples work well together: tuples serve as dictionary keys for composite data, and dictionaries are the fastest way to look up values by key in Python.
Frequently Asked Questions
Can you add items to a tuple in Python?
No, tuples do not support append(), insert(), or any mutation method. The only way to “add” items is to create a new tuple: new_tuple = old_tuple + (new_item,). If you need to frequently modify a collection, use a list instead.
What is the difference between a list and a tuple in Python?
The core difference is mutability: lists are mutable (you can change, add, remove items), tuples are immutable (fixed after creation). This affects where each can be used — tuples work as dictionary keys and in sets; lists do not. Lists have many mutation methods; tuples have only two methods (count and index). In general, use tuples for fixed data and lists for collections that change.
Why use tuples instead of lists in Python?
Three main reasons: Safety — immutability is a contract that the data will not change, useful when passing data between functions. Hashability — tuples can be dictionary keys and placed in sets; lists cannot. Performance — tuples are slightly faster to create and use less memory than lists, which matters in large data structures or hot code paths.
Can a tuple contain a list?
Yes. A tuple can contain any Python object, including lists, dicts, and other mutable objects. The tuple’s own structure is immutable, but the mutable objects inside it can still be modified: (1, [2, 3], 4) is a valid tuple. If you truly need nested mutability with a fixed outer structure, a tuple of lists is appropriate.
What is named tuple in Python?
A named tuple is a subclass of tuple created with collections.namedtuple(). It adds named fields so you can access items by name (point.x) instead of just by index (point[0]). It is fully immutable like a regular tuple, making it ideal for lightweight data records like database rows or coordinate points.

