0

As the question states. map(f, iterable) can be written as [f(x) for x in iterable]. Which is better to use? And why?

As an example, I would like to convert a list of strings to int.

ip = (raw_input().split())
ip = [int(x) for x in ip]

or

ip = (raw_input().split())
ip = map(int, ip)
varuponyou
  • 3
  • 1
  • 3

1 Answers1

1

map may be microscopically faster in some cases (when you're NOT making a lambda for the purpose, but using the same function in map and a listcomp). List comprehensions may be faster in other cases and most (not all) pythonistas consider them more direct and clearer.

more clearly explained here Python List Comprehension Vs. Map

Community
  • 1
  • 1
  • Since you're only copying the first paragraph of Alex Martelli's answer you linked to, it would have been more appropriate to suggest closing the question as a duplicate. Not sure if your reputation is sufficient to do that, so I did it for you. Thanks for finding the canonical answer! – Tim Pietzcker Mar 14 '15 at 05:47
  • I didn't know that, recently started answering here. Will check that out. Thanks. – chanakya gujju Mar 14 '15 at 06:05