85 – Add lists together, fast#
Adding (concatenating) two lists together is a “slow” operation because it requires creating a new list and “copying” the values from the two source lists to the third resulting list.
In performance-sensitive scenarios, it may be better to use itertools.chain to chain the two lists together, instead.
For example, suppose there’s a scenario where you have a node from a tree-like structure and you need to traverse the tree up, from the current node up until the root.
You might write a for loop like the one below:
for node in [this_node] + list(this_node.parents):
pass
If this were performance-sensitive code, you’d be better off using chain:
from itertools import chain
for node in chain([this_node], this_node.parents):
pass
The second alternative is faster because:
this_node.parentsdoesn’t need to be converted to a list; andchaining doesn’t copy values to a third container.
This piece of advice is generally applicable but if you’re writing truly performance-sensitive code, don’t forget to benchmark this change.
Further reading: