# Is there a performance difference between Tuple and List?

Posted on

#### Question :

In Python, I know there is a difference between a `Tuple` and a `List` . The `Tuple` is immutable, and the `List` , changeable.

You may already have this question here: What is the main difference between a Tuple and a List?

However, I’d like to know if in terms of performance one might be better than the other.

Does%% of the%, because it is immutable, can generate more performance / performance than if it were to use `Tuple` ?

Tuples are simpler because they are immutable and for other reasons.

No SO has an answer from Mark Harrison which already shows how Python’s bytecode is mounted in each case and you can see that Tupla is more efficient.

``````>>> def a():
...     x=[1,2,3,4,5]
...     y=x[2]
...
>>> def b():
...     x=(1,2,3,4,5)
...     y=x[2]
...
>>> import dis
>>> dis.dis(a)
2           0 LOAD_CONST               1 (1)
3 LOAD_CONST               2 (2)
6 LOAD_CONST               3 (3)
9 LOAD_CONST               4 (4)
12 LOAD_CONST               5 (5)
15 BUILD_LIST               5
18 STORE_FAST               0 (x)

3          21 LOAD_FAST                0 (x)
24 LOAD_CONST               2 (2)
27 BINARY_SUBSCR
28 STORE_FAST               1 (y)
31 LOAD_CONST               0 (None)
34 RETURN_VALUE
>>> dis.dis(b)
2           0 LOAD_CONST               6 ((1, 2, 3, 4, 5))
3 STORE_FAST               0 (x)

3           6 LOAD_FAST                0 (x)
9 LOAD_CONST               2 (2)
12 BINARY_SUBSCR
13 STORE_FAST               1 (y)
16 LOAD_CONST               0 (None)
19 RETURN_VALUE
``````

answer from Alex Martelli , who is a Python expert, in another question says tuples are built more quickly.

``````\$ python3.1 -mtimeit -s'x,y,z=1,2,3' '[x,y,z]'
1000000 loops, best of 3: 0.379 usec per loop
\$ python3.1 -mtimeit '[1,2,3]'
1000000 loops, best of 3: 0.413 usec per loop

\$ python3.1 -mtimeit -s'x,y,z=1,2,3' '(x,y,z)'
10000000 loops, best of 3: 0.174 usec per loop
\$ python3.1 -mtimeit '(1,2,3)'
10000000 loops, best of 3: 0.0602 usec per loop

\$ python2.6 -mtimeit -s'x,y,z=1,2,3' '[x,y,z]'
1000000 loops, best of 3: 0.352 usec per loop
\$ python2.6 -mtimeit '[1,2,3]'
1000000 loops, best of 3: 0.358 usec per loop

\$ python2.6 -mtimeit -s'x,y,z=1,2,3' '(x,y,z)'
10000000 loops, best of 3: 0.157 usec per loop
\$ python2.6 -mtimeit '(1,2,3)'
10000000 loops, best of 3: 0.0527 usec per loop
``````

The fact that they are immutable gives the right to the codes that access it does not need to make certain checks, there are some more guarantees. This saves time.

There is another answer in the OS of Glenn Maynard which shows how Python internally manipulates the list:

``````case BINARY_SUBSCR:
w = POP();
v = TOP();
if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
/* INLINE: list[int] */
Py_ssize_t i = PyInt_AsSsize_t(w);
if (i < 0)
i += PyList_GET_SIZE(v);
if (i >= 0 && i < PyList_GET_SIZE(v)) {
x = PyList_GET_ITEM(v, i);
Py_INCREF(x);
}
``````

This makes access, which is what matters most, practically the same thing.

Tuples also take up less space, which can help with data caching.

There may be other implications. Each situation can have a different effect. But the question is not clear as to what situation it is talking about.

The conclusion is the same as always. Use whatever is best for each situation and do not worry about performance. In this case, as in many, the difference is minimal and if you want performance even, do in C, C ++ or another language where performance can be the maximum.