1

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
  • 13
  • 1
  • 1
  • 4
  • Found the link: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 where Guido van Rossum explains why he wanted to drop `map`, `filter`, `lambda` and `reduce` from the language. – Tim Pietzcker Mar 14 '15 at 05:43
  • When you say "complexity", are you talking about the computer science concept (big-O notation)? Or do you mean "complexity of code"? – Karl Knechtel Mar 14 '15 at 06:01
  • @KarlKnechtel I meant the computer science concept! – varuponyou Mar 14 '15 at 08:58

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