r/learnpython • • Feb 20 '21

What are you having difficulty learning or are currently struggling to learn?

I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.

I'm looking forward to hearing what you guys have to say!

417 Upvotes

324 comments sorted by

View all comments

Show parent comments

3

u/remuladgryta Feb 21 '21 edited Feb 21 '21

Lambdas are a concept borrowed from functional (as in mathematical function) programming. Programmers who write in functional programming languages love when they spot a pattern in their program because then they get to write a general version of the pattern and never worry about writing similar code again.

An example is one time long ago a functional programmer noticed that they had a lot of functions that took a list as a parameter and returned a new list where each element of the result was based on each element of the parameter.

def square_list(lst):
    return [x**2 for x in lst]

def add_ten_list(lst):
    return [x+10 for x in lst]

def halve_list(lst):
    return [x/2 for x in lst]

This made the functional programmer very excited. "Look, all these functions do exactly the same thing except for one little part! I bet I can write a general list transforming function." the functional programmer thought. And so they did:

def transform_list(operation, lst):
    return [operation(x) for x in lst]

Now that they had a general version of their function, they could rewrite their old ones like this:

def square(x):  
    return x**2

def add_ten(x):
    return x + 10

def halve(x):
    return x / 2

def square_list(lst):
    return list_transform(square, lst)

def add_ten_list(lst):
    return list_transform(add_ten, lst)

def halve_list(lst):
    return list_transform(halve, lst)

But wait, that's kinda cluttered. Now you have all these functions square, add_ten, and halve that have names even though you only care about them in the context of their corresponding blah_list function. You could move the helper functions into the function bodies of the "real" functions to hide some of that clutter from the rest of the program like so:

def square_list(lst):
    def square(x):
        return x**2
    return list_transform(square, lst)

That's a bit better, but what's the point of declaring a function and giving it a name if the sole reason for that function existing is to be a behavioral parameter to some other function? That's the problem lambdas aim to help with. Lambdas are "throwaway" functions without a name and more concise notation, perfect for using smack dab in the middle of a function parameter list. The same list functions written using lambdas:

def square_list(lst):
    return list_transform(lambda x: x**2, lst)

def add_ten_list(lst):
    return list_transform(lambda x: x+10, lst)

def halve_list(lst):
    return list_transform(lambda x: x/2, lst)

I've been calling the general function list_transform all this time, but in reality there is an even more general version in the python standard library called map. map behaves similarly to list_transform except it returns an iterator instead of a list and it can take more than just one iterable (lists are an example of iterable objects) as input. For example if you have two lists a and b and you want to add them together element-wise you could write something like this

def add_two_lists(a, b):
    """ 
    Add the elements of a to the elements of b, resulting in a list c.
    Discards elements from the longer list that don't have a match in the shorter.
    """
    shortest_length = min(len(a), len(b))
    return [a[i] + b[i] for i in range(shortest_length)]

but with map you could instead write it like this

def add_two_lists(a, b):
    """ 
    Add the elements of the list a to the elements of the list b, resulting in a list c.
    Discards elements from the longer list that don't have a match in the shorter.
    """
    return list(map(lambda x, y: x+y, a, b))

map is a useful function, but in python it is often more natural to write these kinds of transformations as list comprehensions:

def add_two_lists(a, b):
    """ 
    Add the elements of the list a to the elements of the list b, resulting in a list c.
    Discards elements from the longer list that don't have a match in the shorter.
    """
    return [x+y for x, y in zip(a, b)]

However, there are tons of useful functions that take another function as one of their parameters to turn a general pattern into a specific one or to slightly modify a default behavior. Examples include but are not limited to the sorted function, the functools module, and the itertools module.

1

u/BlueBombadier Feb 21 '21

Holy crap this is a great explanation. For some reason I had never seen or thought of using a lambda in a function call. This helped immensely, thank you!