DEV Community

Vicki Langer
Vicki Langer

Posted on • Updated on

Charm the Python: Lambdas

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.


Lambda Function

Lambda functions are unnamed functions. This means it is not defined like a normal function.

Defining lambdas is similar to defining a regular function, but it has a different syntax.

# syntax of a function
def name(args):

# syntax of lambda function
lambda args: expression  # can only have one expression
lambda args, args, args: expression  # may have multiple parameters
Enter fullscreen mode Exit fullscreen mode

Note: Lambdas can only handle expressions. Expressions must have return values like numbers, True, and False

Lambda, by Itself

# A regular function
def add_two_numbers(a, b):
    return a + b

# Same function written as a lambda function
add_two_numbers = lambda a, b: a + b
Enter fullscreen mode Exit fullscreen mode

Notice, both take args a and b, then add them together. The only difference is that the first one is defined as a function.

For my math loathing pals

# A regular function
def generate_full_name(first_name, last_name):
    return first_name + ' ' + last_name

# Same function written as a lambda function
generate_full_name = lambda first_name, last_name: first_name + ' ' + last_name
Enter fullscreen mode Exit fullscreen mode

Lambda function inside other function

Lambda functions can be used inside of other functions.

# future home of an example
# if you could help me with a simple example that would be badass
# a non-math example would be appreciated forever
# simple math would be cool too (something like 2+2)
Enter fullscreen mode Exit fullscreen mode

Further Investigation

If all of this makes sense to you and you'd like a little more fanciness and excitement, lookup 'Self invoking lambda function'


Note: Lambdas are not something I'm really comfortable with. For more details check out this thorough article about Lambdas in Python.


Series based on

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

For your example of a "function inside another function", do you mean something like this?

my_list = (
    {
        "name": "Ben",
        "age": "99"
    },
    {
        "name": "Antigone",
        "age": "49"
    },
    {
        "name": "Jonas",
        "age": "10"
    },
    {
        "name": "Samantha",
        "age": "52"
    },
    {
        "name": "Vicki",
        "age": "22"
    }
)

print (sorted(my_list, key = lambda x: x["age"]))
Collapse
 
vickilanger profile image
Vicki Langer

I think this is what I was referring to, but I’m not super sure. All the examples I saw were using functions like map(), filter(), and sort()

I’ve never seen key used before. Help me out, what is x doing in this?

If I’m reading this correctly, it’s just sorting these key-value pairs by age, right?

Collapse
 
moopet profile image
Ben Sinclair

yes, array.sort and sorted take a named parameter in Python 3 (I had to learn this a couple of days ago!) which is run as a callback, once per key, immediately before sorting the elements. So that example calls x: x["age"] on every element first.
It's the equivalent of this:

sort([x["age"] for x in my_list])

and this:

def by_age(x):
    return x["age"]

sort(my_list, key = by_age)