Optimization in Python - tuples vs lists
In Python, tuples and lists are two similar data structures used to store sequential data. The well known difference between the two is that tuples are immutable whereas lists are not.
Tuples vs Lists
Despite the fact that tuples are comparatively less popular than lists, they find its application in many crucial aspects of Python, like
- returning more than 1 item from a function
- dictioanry key and value pairs
- arguments and parameters
A normal Python program has thousands of tuples and lists
Here I have used the Python module called gc
which stands for Garbage Collector. It underlines the underlying memory management function of Python, the automatic garbage collector. The get_objects()
method returns a list of all objects tracked by the collector, excluding the list returned.
Empty lists vs empty tuples
There is a big difference when considering empty lists and tuples. An empty tuple acts as a singleton but it isn’t true for lists. While creating an empty tuple, Python points to the already preallocated one in such a way that both of them has the same address, thereby saving memory.
For lists, this doesn’t hold
Allocation
To reduce memory fragmentation and allocation time complexity, Python reuses old tuples. Instead of permanently deleting a tuple, Python moves it to a free list
if the tuple has less than 20 items.
Lists also use the same optimization method as tuples.
List resizing
To avoid the cost of resizing, Python does not resize a list everytime we add or remove an item. Instead, it over-allocates memory to a list in the form of empty slots
which are hidden from the user. The documentation describes it as follows
This over-allocates proportional to the list size, making room for additional growth. The over-allocation is mild but is enough to give linear-time amortized behavior over a long sequence of appends() in the presence of a poorly-performing system realloc().
The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, …
For example, if you want to append an item to a list of length 8, Python will resize it to16 slots and add the 9th item. The rest of the slots will be hidden and reserved for new items.
Tuples do not over-allocate
Unlike lists, as we saw above, tuples do not use over-allocation. They are of fixed size and can store data more compactly.
Lists have faster append
Due to lists over-allocation, it reduces the cost of append operation as Python doesn’t need to allocate it memory at that time. Hence the append operation of a list is faster than tuple’s. I wrote a script to check it.
On executing the script, the results were as I expected
Tuples do not need to be copied
Since tuples are immutable, they do not have to be copied. Rather, Python just refers the memory address of the old tuple to the new one.
This is not true for lists.
Tuples can be constant folded
Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime. Lists, on the other hand are evaluated at the runtime and need to be build up from scratch. This significantly reduces the time complexity of operations using tuples.