3 ms·
You can decompose any list comprehension into it's equivalent for loop fairly easily. I don't see what is so magic about them other than the language having a
by novok 7y ago
You can decompose any list comprehension into it's equivalent for loop fairly easily. I don't see what is so magic about them other than the language having a special compiler level optimization for them?
g = [i for i in list if x == 3] -> g = []; for i in list: if x == 3: g.append(i);
- mplanchard 7y agoI see people frequently say that list comprehensions are special-cased and highly optimized by the compiler, but I did a few experiments recently with `timeit` and found that using `map()` is on the order of 1.5x faster in all the cases I tested. I think that either winds up being faster than a manual for loop, though