3 ms·
Python does have a special optimization: when the iterable in a comprehension has len() defined, then Python preallocates the list. ok, that makes sense then.
by dec0dedab0de 5y ago
Python does have a special optimization: when the iterable in a comprehension has len() defined, then Python preallocates the list.
ok, that makes sense then. So when I made a generator it didn't get the preallocation advantage, but range did because the range object has len.
The comprehension is faster than manual preallocation+filling because the indexing happens in C instead of in Python.
I learned this a long time ago, and have tried to use comprehensions since, To the point where I had to draw a line for readability because it was annoying co-workers. Though I never really took the time to understand what "In C" actually meant beyond it being an optimization over using a for loop with append.
Do you happen to know why list(map(func, iterable)) is faster than [func(_) for _ in iterable] ? I assume something about calling back to a python function from C, but my knowledge of the internals is obviously lacking at these levels.