JP van Oosten

tuple vs list

04 Aug 2008

Everyone with a bit of python knowledge can intuitively feel that tuples are faster than lists. Of course: tuples are unmutable, and therefore should be faster. I was wondering today, how much faster tuples were.

Again, we use the timeit module to measure runtime of a command.

$ python -m timeit '[True]'
1000000 loops, best of 3: 0.206 usec per loop

$ python -m timeit '(True,)'
10000000 loops, best of 3: 0.118 usec per loop

So, using a tuple in such a simple case is almost 1.8 times faster!

(I like the fact that I can test my intuition with timeit)