Python List vs Tuple
Lists and tuples both store ordered sequences, and both support indexing, slicing and loops. The difference that matters is that a list can change and a tuple cannot. That one difference decides which to use.
- The key differences at a glance
- Memory and speed compared
- Why tuples can be dict keys
- A simple rule for choosing
Differences at a glance
| List | Tuple | |
|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Mutable | Yes: add, remove, change items | No |
| Methods | Many: append, remove, sort… | Two: count, index |
| Can be a dict key / set item | No | Yes, if its items are hashable |
| Memory | More | Less |
| Typical use | A collection that grows or changes | A fixed record of values |
Mutability in practice
cart = ["pen", "book"]cart.append("bag")cart[0] = "pencil"print(cart) location = (18.52, 73.86)print(hasattr(location, "append"))['pencil', 'book', 'bag'] False
Memory and speed
A tuple uses less memory than a list with the same items, because a list keeps spare room so it can grow.
import sys items_list = [1, 2, 3, 4, 5]items_tuple = (1, 2, 3, 4, 5)print(sys.getsizeof(items_list), sys.getsizeof(items_tuple))104 88
Creating a tuple of constants is also faster, because Python can build it once when the code is compiled:
import timeit print(timeit.timeit("[1, 2, 3, 4, 5]", number=1_000_000) > timeit.timeit("(1, 2, 3, 4, 5)", number=1_000_000))True
The exact sizes and timings depend on your Python version and computer. The direction does not: tuples are smaller and faster to create.
Hashability: tuples as keys
Only immutable objects can be dictionary keys or set members.
visited = {(0, 0), (0, 1), (1, 1)}print((0, 1) in visited) grid = {(2, 3): "wall", (4, 1): "door"}print(grid[(4, 1)])True door
visited = {[0, 0], [0, 1]}Traceback (most recent call last):
File "main.py", line 1, in <module>
visited = {[0, 0], [0, 1]}
^^^^^^^^^^^^^^^^
TypeError: cannot use 'list' as a set element (unhashable type: 'list')Converting between them
t = (3, 1, 2)lst = list(t)lst.sort()t = tuple(lst)print(t)(1, 2, 3)
Which should you use?
- Use a list when the collection will change: items are added, removed or sorted, such as a shopping cart or a queue of test cases.
- Use a tuple when the values belong together and should not change, such as
(latitude, longitude), an RGB colour, a function returning several values, or a dictionary key.
Check your understanding
Quick quiz
1. Which can be used as a dictionary key?
Lists are mutable and therefore unhashable.
2. Which uses less memory for the same five numbers?
A list keeps extra capacity so it can grow.
3. You need to store an employee’s (id, name, joining date) record that never changes. Which fits best?
It is a fixed record, so a tuple communicates intent and protects it from accidental changes.
Program Practice
70 Python Tuple Programs
Every program has a step-by-step explanation, colour-coded code and its real output.
List vs tuple FAQ
Is a tuple faster than a list in Python?
Creating and iterating over a tuple is slightly faster and it uses less memory. The difference rarely matters; choose based on whether the data should change.
Why use a tuple instead of a list?
To signal that the data is fixed, to protect it from accidental changes, to use it as a dictionary key or set member, and to save a little memory.
Can a tuple contain a list?
Yes. The tuple cannot be changed, but the list inside it can. Such a tuple is not hashable, so it cannot be a dict key.
2 thoughts on “Python List vs Tuple”