4 ms·
Don't know if this is why, but the list comprehension takes an expression at the "foo(word)" location, and is therefore more general than map, which requires a
by a3n 12y ago
Don't know if this is why, but the list comprehension takes an expression at the "foo(word)" location, and is therefore more general than map, which requires a function. The comprehension in that case is simpler.
words = ['w1', 'w2', 'w3']
[word[1] for word in words]
['1', '2', '3']
map(lambda x: x[1], words)
['1', '2', '3']
I like looking at the list comprehension better. The use of lambda looks forced in this case. I also imagine there's a penalty for calling the (anonymous) function in map.
- icebraining 12y agoIn that case I'd use map(itemgetter(1), words)
- a3n 12y agoInstead of my map example, or instead of a list comprehension in general?
- icebraining 12y agoInstead of a list comprehension equivalent to your example.