[Tutor] lambda vs list comp

Mac Ryan quasipedia at gmail.com
Tue Jul 27 18:53:45 CEST 2010


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.



More information about the Tutor mailing list