lambda functions

Rhodri James rhodri at wildebst.demon.co.uk
Mon Aug 31 18:40:12 EDT 2009


On Mon, 31 Aug 2009 08:41:57 +0100, Pierre <pierre.gaillard at gmail.com>  
wrote:

> Hello,
>
> I would like to know if it is possible to define a loop in a lambda
> function....
>
> How to manage the indents ? Example :
> s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
> [index]-1

You can't use commands in a lambda function, only expressions.  The nearest
thing I can think of to what you want here is to use a list comprehension:

   s_minus_1 = lambda s : [i-1 for i in s]
   my_list = s_minus_1(my_list)

On the other hand, giving a name to an anonymous function rather defeats
the point of it being an *anonymous* function!  What are you actually
trying to do here?  There's almost certainly a better way to do it than
this.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list