A tiny question regarding lambdas

I’ve been doing the Data Science career path in Codecademy and learnt a while ago about lambdas. I started a new section of the career path and suddenly lambdas are written with a backslash " \ "at the end of each line. But the course never explained it before. What is that??

age_check = lambda x: 'Welcome to the site!' \
    if age > 13 \
    else 'Sorry you are too young to enter this site.'

has nothing to with lambdas, the backlashes allow us to put the code on multiple lines.

That’s great! Thanks!

Pointless lambda. Write this instead:

def age_check(x):
    if age > 13:
        return 'Welcome to the site!'
    return 'Sorry you are too young to enter this site.'

I see a lot of people around here who talk as if lambda isn’t creating a function, which blows my mind, if they’re not aware of that then what do they think they’re doing?

As for backslashes, for most cases you should be using parentheses around the thing you’re splitting up instead.

age_check = (
    lambda x: "Welcome to the site!"
    if age > 13
    else "Sorry you are too young to enter this site."

Which does nothing to excuse using lambda in the first place, use a def statement.

What’s particularly disturbing is the suggestion that a lambda would have something that other function’s don’t, without knowing what that is. You want a function. A function. Input goes in, output goes out.

Also, the argument x isn’t used and there’s an undefined name age.


I have wondered for awhile what the point of a lambda is, since it seems to work the exact same as a function, and I can not find any documentation or examples that indicate otherwise.

I want to make sure I am not misunderstanding you though,

Are you saying that lambdas are pointless? Or that we can use functions just as well instead?

The point is that they’re expressions, so you can define a function wherever you need one.

That lambda is pointless because it’s just emulating a def statement.

1 Like

Thanks for the clarification, very good to know :grinning:


I primarily only use lambdas for things like creating a new Pandas DataFrame column out of info contained within the DataFrame.

For example, let’s say you have a DataFrame called schools with a column called languages that lists the language classes (if any) that each school has. You might want to include a column has_lang in your data that flags each school that has language classes with a 1 and those that don’t with a 0.

You can easily do this with a single line using a lambda:

schools['has_lang'] = schools['languages'].apply(lambda x: 0 if pd.isnull(x) else 1)

Outside of this context I rarely use them. Also, depending on your use case, it still may be better to pass a regular function to .apply():

def find_borough(x):
    if x[2] == 'M':
        return 'Manhattan'
    elif x[2] == 'K':
        return 'Brooklyn'
    elif x[2] == 'X':
        return 'Bronx'
    elif x[2] == 'Q':
        return 'Queens'
        return 'Staten Island'
df['borough'] = df['DBN'].apply(find_borough) 
1 Like

lambda calculus is a different strategy of representing and evaluating a program

python’s lambda doesn’t do any of that, it’s just an expression for defining a function to accommodate for one-off uses that would be pointless to name, same as string or number literals

from dis import dis

def f(x):
    return x + x + x

dis(lambda x: x + x + x)

their compiled representations are the same

  5           0 LOAD_FAST                0 (x)
              2 LOAD_FAST                0 (x)
              4 BINARY_ADD
              6 LOAD_FAST                0 (x)
              8 BINARY_ADD
             10 RETURN_VALUE
 10           0 LOAD_FAST                0 (x)
              2 LOAD_FAST                0 (x)
              4 BINARY_ADD
              6 LOAD_FAST                0 (x)
              8 BINARY_ADD
             10 RETURN_VALUE

if you were to dig into mail archives there’s probably some lisp programmer there who made a function to evaluate strings as a python expression and named that function lambda and then that was kept when it was made a feature.
it’s definitely from functional programming, but so are many other features

two bigger pieces of that puzzle are that functions are values, and functions can be created, computed, by the program itself

@ionatan Duly noted and withdrawn!! Thanks for the correction!