Python List vs Tuple

Python Tutorial › Data Structures › List vs Tuple

Python List vs Tuple

BeginnerLesson 21 of 477 min read6 runnable examples

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.

In this lesson
  • 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

ListTuple
Syntax[1, 2, 3](1, 2, 3)
MutableYes: add, remove, change itemsNo
MethodsMany: append, remove, sort…Two: count, index
Can be a dict key / set itemNoYes, if its items are hashable
MemoryMoreLess
Typical useA collection that grows or changesA fixed record of values

Mutability in practice

Python
cart = ["pen", "book"]cart.append("bag")cart[0] = "pencil"print(cart) location = (18.52, 73.86)print(hasattr(location, "append"))
Output
['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.

Python
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))
Output
104 88

Creating a tuple of constants is also faster, because Python can build it once when the code is compiled:

Python
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))
Output
True
Note

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.

Python
visited = {(0, 0), (0, 1), (1, 1)}print((0, 1) in visited) grid = {(2, 3): "wall", (4, 1): "door"}print(grid[(4, 1)])
Output
True
door
Python
visited = {[0, 0], [0, 1]}
Output
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

Python
t = (3, 1, 2)lst = list(t)lst.sort()t = tuple(lst)print(t)
Output
(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?

2. Which uses less memory for the same five numbers?

3. You need to store an employee’s (id, name, joining date) record that never changes. Which fits best?

Program Practice

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.