0

When comparing these, which do you think is more intuitive / easier to read?

>>> [ord(i) for i in 'some string']
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]

>>> map(ord,'some string')
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]

Is there any benefit to the lambda/map way?

tMC
  • 18,105
  • 14
  • 62
  • 98
  • 1
    I like first one better, easier to read. – Davor Lucic May 26 '11 at 21:34
  • 3
    I seem to be a lone voice here, but I think the second is much more readable. `i` has no actual semantic content in the first example, so the second is shorter (frequently better by itself), and it also has one fewer thing to keep track of. This is much clearer when the objects have semantic content so you're tempted to use more verbose variables to keep your code readable: eg `[get_foo_for_barspam(barspam) for barspam in barspams]` That said, there are advantages to comprehensions (eg tuple unpacking) and they're much more flexible than map (though see also `itertools.starmap`). – Lucas Wiman May 27 '11 at 07:19
  • I find map more intuitive. But its very subjective, I knew about map before I knew about list comprehensions, which is probably why. I find RecursivelyIronics point about the i's lack of semantic value has merit. Also, the map way is shorter. A length argument is pretty lame, but it is at least objective. – Sean Feb 28 '13 at 14:39
  • It should be noted that the performance numbers in the OP of the linked question are out of date. Recent versions have, apparently, dramatically improved the relative performance of `map` vs. comprehensions. And yes, I did account for the need to explicitly list-ify the results. – Karl Knechtel Jan 28 '15 at 10:26

4 Answers4

5

I like the first one better and there's an advantage to it. You can instead of

[ord(i) for i in 'some string']

write

(ord(i) for i in 'some string')

and have a generator instead of making a list. Great in some siutations.

dfb
  • 13,133
  • 2
  • 31
  • 52
4

I prefer the first one, because I'm more used to it. Somebody doing a lot of functional programming will probably prefer the second one, because it better fits it's mind set. But the second version is to complicated. It can be reduced to

map(ord,'some string')

Which is much more readable and makes the idea more obvious.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Achim
  • 15,415
  • 15
  • 80
  • 144
  • +1, but list comprehensions are also functional programming constructs are they not? They are in Haskell anyway. – Sean Feb 28 '13 at 14:44
0

I would use: [ord(i) for i in 'some string']. Using map with a lambda is supposedly much slower then using list comprehensions. See Python List Comprehension Vs. Map The first one is also more readable in my opinion.

Community
  • 1
  • 1
garnertb
  • 9,454
  • 36
  • 38
  • `[ord(c) for c in 50 * ''.join(chr(i) for i in range(256))]` is about 33% slower than `map(ord, 50 * ''.join(chr(i) for i in range(256)))` (Try it!) – Lucas Wiman May 27 '11 at 07:07
  • 1
    I believe the OP modified the question, I remember the second option having a lambda in it. – garnertb May 27 '11 at 11:12
0

This seems to have been asked and answered before

Community
  • 1
  • 1
Asgeir
  • 1,092
  • 1
  • 7
  • 11
  • i figured it was- i looked for it, but my searching-fu is less than awesome. thanks – tMC May 26 '11 at 21:36