Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


In that case I'd use

  map(itemgetter(1), words)


Instead of my map example, or instead of a list comprehension in general?


Instead of a list comprehension equivalent to your example.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: