DEV Community

Discussion on: Choosing the Best Iteration Technique for Optimized Python Code

Collapse
 
aminmansuri profile image
hidden_dude

You may very well be right, but I suspect that your testing methodology may have introduced some extra overhead. Mainly in the allocation of the list.

The higher order function calls list() at the end which makes a full copy of the entire result. The for-iteration is calling append repeatedly whereas the comprehension may have some trickery to allocate a list of the right size.

It would be useful to try to factor out the list allocation aspect of your test because those subtle differences are key.