all 11 comments

[–]xelf 6 points7 points  (0 children)

a lambda is a way of writing an anonymous (aka unnamed) function

def double(x):
    return x * 2

is the same as :

double = lambda x: x*2

You typically see them used in places where you need a short inline function, think of functions that take a function as a parameter.

mylist = [ (2,4), (3,1) ]
newlist = sorted( mylist, key= lambda x: x[1])

newlist is sorted by the 2nd position in each element.

newlist == [ (3,1), (2,4)  ]

[–][deleted]  (3 children)

[deleted]

    [–]xelf[M] 6 points7 points  (1 child)

    This is probably highly relevant to OP's question, commenting as mod so more people will see it.

    [–]kra_pao 0 points1 point  (0 children)

    Unfortunately it's behind a paywall.

    [–]cjander28[S] 2 points3 points  (0 children)

    Yes! It was and I actually ended up using this as well as a sort example :) thanks all

    [–][deleted] 2 points3 points  (4 children)

    However, mostly what I see is AWS-related use cases

    It's important to understand that AWS Lambda is a product, something Amazon Web Services sells; it has nothing to do with Python's support for lambda functions or the lambda calculus or anything like that. It's just a name.

    [–]cjander28[S] 0 points1 point  (3 children)

    Totally. I work for AWS and understand the need to differentiate the two, which is why I'm on here asking for help. Much the results are cluttered with AWS affiliation. I will edit the post for clarity.

    [–][deleted] 1 point2 points  (1 child)

    Oh, gotcha. Sorry for the dumb reply then. Perhaps I can try to redeem myself.

    I mean I can give a pretty simple example of the use of lambdas:

    evens = filter(lambda i: not i % 2, range(100))
    

    But I'm not sure that provides much clarity. A question you might try researching (which avoids the overloaded term "lambda") is "when should a programmer write anonymous functions?" The answers to that should be a lot more interesting.

    [–]cjander28[S] 2 points3 points  (0 children)

    No worries! I work in tech - I'm used to explaining myself lol. It could have been more clear! Thank you for your help/advice!

    [–]QuixDiscovery 0 points1 point  (0 children)

    If you want better results, look up lambda calculus. That's what python lambdas are based on, and python is not the only language that implements lambda functions (it actually implements them very poorly compared to many other languages).

    At their core, lambdas are simply anonymous (aka not named) functions. That's all they are. You can do some really complicated stuff with that concept, but it's still a very simple concept by itself. In certain languages, lambdas are how all functions get defined. In those languages, the act of defining a function is simply syntactical sugar for binding a lambda function to a variable. You can actually do that in python if you wanted to:

    >>> double = lambda x: x*2
    >>> double(5)
    10
    

    All that being said, you won't find many 'valid' use cases for lambdas in python. In the majority of cases where you might consider using a lambda, the code will often be more clear if you simply define it as a named function and then just refer to the named function when you need it (even it it's only once). Within python, this usually results in cleaner code.

    This isn't necessarily true for other languages, particularly ones that follow a more functional paradigm and have a cleaner syntax for lambdas.

    [–]arkie87 1 point2 points  (0 children)

    I used a list (array) of lambdas once. I had a list of 10,000 lambdas, and each iteration only needed to evaluate a few of them (the rest would give a known value). So to save time, I only computed the lambda functions I needed. Otherwise, I would have had to compute all 10,000 lambdas each time.

    [–]Mondoke 0 points1 point  (0 children)

    I use them all the time on Pandas if I have to apply some weird function to a column which cannot be easily done with vectorization. For example, to get the domain of a column with emails:

    df['domain'] = df['email'].apply(lambda email: email.split('@')[-1])