Re: [Tutor] lambda vs list comp

2010-07-27 Thread Steven D'Aprano
On Wed, 28 Jul 2010 02:44:02 am Jon Crump wrote:
> Just as a matter of curiosity piqued by having to understand someone
> else's code. Is the difference here just a matter of style, or is one
> better somehow than the other?

Remember that list comprehensions didn't exist until a few years ago, so 
older code will use map() to implement the same functionality.

> >>> l
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using lowercase L on its own as a name is not recommended, because it 
looks too similar to 1 and hence difficult to read. For example, the 
next line reads to me like "blah blah for x in ONE", I do a 
double-take, read it again, and realise it's actually L.


> >>> ','.join([str(x) for x in l])
> '0,1,2,3,4,5,6,7,8,9,10'

This would be the preferred (or "Pythonic") way to do this for most 
people these days. That, or a generator expression:

','.join(str(x) for x in l)

Using the list comp form is a micro-optimization over the generator 
expression form (it allows join to allocate the memory needed ahead of 
time, since it has a known size).


> >>> ','.join(map(lambda x:str(x), l))
>
> '0,1,2,3,4,5,6,7,8,9,10'


That is a line of code written by somebody who doesn't quite get it.

There seems to be something about map and lambda that leads so many 
people into making that silly mistake when they wouldn't make it 
elsewhere. You (generic you, not you personally) almost certainly 
wouldn't write:

def mystr(x):
return str(x)

n = 42
s = mystr(n)


instead of the more obvious s = str(n). But bring map() into it, and 
people make that exact mistake, writing:

map(lambda x: str(x), L)

instead of the simpler and faster:

map(str, L)



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lambda vs list comp

2010-07-27 Thread Rich Lovely
On 27 July 2010 17:53, Mac Ryan  wrote:
> On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote:
>> Just as a matter of curiosity piqued by having to understand someone
>> else's code. Is the difference here just a matter of style, or is one
>> better somehow than the other?
>>
>> >>> l
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>
>> >>> ','.join([str(x) for x in l])
>> '0,1,2,3,4,5,6,7,8,9,10'
>>
>> >>> ','.join(map(lambda x:str(x), l))
>> '0,1,2,3,4,5,6,7,8,9,10'
>
> Considering that "readability metters" I would go for none of the two,
> but for:
>
> ','.join(map(str, l))
>
> (Indeed defining a lambda function that simply uses "str" is redundant)
>
> Just my personal take on this, though!
> Mac.
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

There's very little difference between the two in the example you're
using, in python 2.x.  It's a different matter if you want to do
something slightly more complex, or of you're using Python 3.

The benefit of list comprehensions is that they can take a number of
iterables and filter clauses:

>>> [x+y for x in range(3) for y in range(3) if x!=y]
[1, 2, 1, 3, 2, 3]

I can't see any easy way of doing something like that using just map,
fliter and so on.

In python 3, map is a different sort of beast.  Instead of returning a
list, it returns an iterable called a "mapping".  It stores the
original list and the function, and only calls the function when you
ask it for the next value.  You can get similar behaviour in python2
using the imap function in the itertools module.  It's extremely
useful if you need to deal with massive lists of millions of elements
- where making the list every time would take too long.

-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lambda vs list comp

2010-07-27 Thread Mac Ryan
On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote:
> Just as a matter of curiosity piqued by having to understand someone
> else's code. Is the difference here just a matter of style, or is one
> better somehow than the other?
> 
> >>> l
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> 
> >>> ','.join([str(x) for x in l])
> '0,1,2,3,4,5,6,7,8,9,10'
> 
> >>> ','.join(map(lambda x:str(x), l))
> '0,1,2,3,4,5,6,7,8,9,10'

Considering that "readability metters" I would go for none of the two,
but for:

','.join(map(str, l))

(Indeed defining a lambda function that simply uses "str" is redundant)

Just my personal take on this, though!
Mac.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] lambda vs list comp

2010-07-27 Thread Jon Crump
Just as a matter of curiosity piqued by having to understand someone
else's code. Is the difference here just a matter of style, or is one
better somehow than the other?

>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> ','.join([str(x) for x in l])
'0,1,2,3,4,5,6,7,8,9,10'

>>> ','.join(map(lambda x:str(x), l))
'0,1,2,3,4,5,6,7,8,9,10'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor