3 ms·
i. let x = [ 1, 2, 3].map(i => i * 10) ii. x = list(map((lambda i: i * 10), [ 1, 2, 3 ])) iii. x = [i * 10 for i in [1, 2, 3]] All these methods are
by SeanDav 10y ago
i. let x = [ 1, 2, 3].map(i => i * 10)
ii. x = list(map((lambda i: i * 10), [ 1, 2, 3 ]))
iii. x = [i * 10 for i in [1, 2, 3]]
All these methods are really just multiple sides of the same coin. All I see is different syntax, none looks any better from my point of view and unless there is a clear performance benefit, just use what you are used to, or what your coding standards define.
- NegativeLatency 10y agoI think in general the comprehension is faster because of how method calls and symbol lookups work in python. For the most part it's just a style thing.
- lyschoening 10y agoii. vs iii. boils down to explicit rather than implicit. iii. is simply easier to understand. I believe most people would also agree that ii. doesn't look particularly nice.