The Lambda Function: Just as simple as it sounds!
If you're a beginner programmer like me, you may have heard the term "lambda" thrown around in python discussions. At first, I was so incredibly confused on what the heck a lambda function was doing. However, fear not! In this blog post, I'll break down the concept of lambda in a way that helped me understand, so you can grasp its significance and how it can be used in your coding journey.
In programming, a lambda function is an anonymous function, meaning it lacks a name, that can be defined on-the-fly. Unlike regular functions, you don't need to assign a name to a lambda function before using it. Instead, you create the function right where you need it, making it incredibly useful for certain scenarios. It's like a little spell that we can use whenever we want!
The syntax of a lambda function is relatively straightforward. It typically follows this pattern:
lambda arguments: expression
Here's a simple example to illustrate the point. Let's say we want to create a function that doubles a given number:
Regular Function:
def double(x):
return x * 2
vs:
Lambda Function:
double_lambda = lambda x: x * 2
As you can see, the lambda function has the same functionality as the regular function, but it's shorter and doesn't require a separate def statement.
But what is the POINT?!? Lambdas are often used in situations where a small, throwaway function is needed. They are commonly used with higher-order functions like map(), filter(), and reduce(). (Higher order meaning the kind of functions that use other functions as arguments.
map():
The map() function applies a given function to all items in an iterable (a list) and returns a new iterable with the results. Here's how a lambda function can be used with
map():
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x**2, numbers)
# Output: [1, 4, 9, 16, 25]
filter():
The filter() function filters elements from an iterable based on a given condition (lambda function) and returns a new iterable with the elements that satisfy the condition:
numbers = [1, 2, 3, 4, 5]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
# Output: [2, 4]
Sorting with sorted():
Lambda functions can also be used to define custom sorting criteria when working with the sorted() function:
names = ['Alice', 'Bob', 'Charlie', 'David']
sorted_names = sorted(names, key=lambda name: len(name))
# Output: ['Bob', 'Alice', 'David', 'Charlie']
I find that using a lambda function during sort() is incredibly useful.
One note about lambda functions is that while they can be incredibly useful and create simplified code, they also have some limitations. One being that lambda functions can only contain one single expression, so you can't use complex expressions with a lambda. Also, because they are a one line anonymous function, they can be a little more difficult to debug. In a big program with lots of lines of code, a small unnamed function can be difficult to parse out as a source of a bug.
I hope this brief introduction to lambda functions proved helpful to you! When using lambda's in higher order functions, your code will be more readable and useful!
Top comments (1)
Nice explanation