How to Train Your Python: Part 14, More Advanced Lists, Lambda, and Lambda Operators
Welcome back! In our last training session, we covered some advanced list topics. We'll be going deeper into the advanced list rabbit hole today with lambda and lambda operators! (And I'm not talking about Gordan Freeman)
We'll start by explaining lambda and giving an example, then we'll cover each of the operators, so, let's get started!
We'll first start by explaining what lambda is. Lambda is a way to make simple functions on the fly. If you haven't read the article on functions, I suggest you do so, otherwise this could get confusing.
When we usually make functions, we have to define them, and then we usually return a result. We don't have to define lambda functions, and we don't have to return any results. We can use lambda to condense simple code into a single line, so instead of making a whole new function, we just throw in a quick lambda. Let's move on the basic lambda example.
In this example, we'll be using defining a simple function, and then we'll do the same thing much quicker with lambda. So, let's look at our function first. We'll be making a simple function to return the result of an integer times 2. Let's make our simple function now:
Alright, we started by defining our function, then we gave it the argument of x, in the body of the function, we multiply x by 2 and return the result! When we called this function with the integer 2 as x, the answer (obviously) is 4.
We can also assign lambda's to variables, making simple functions very fast. Let's make our lambda function now:
We can see here that in order to use a lambda expression as an actual function, we need to assign it to a variable, which can still be faster than defining a new function. We use lambda by entering lambda, followed by the name of our argument, in this case x. Then, we enter the code to perform on the argument(s), which in this case is multiplying it by 2. Now that we know a little more about lambda's and how to create them, let's look at lambda operators.
We'll review lambda operators one by one. They're each useful in their own way. Let's start by listing them and giving a brief explanation of each.
- filter() - This can be used to filter certain list elements into a new list.
- map() - This can be used to execute code on all elements of a list at once.
- reduce() - This can be used to perform arithmetic sequentially through a list.
As a side note: These operators can be used as individual functions, but we will mostly see them used in tandem to lambda expressions.
Now that we've covered these operators in brief, let's get into more detail. We'll cover each of them. So, let's get started!
Let's cover how we can use the filter() operator. We can use this operator if we want to filter specific contents of a list into a new list. We'll be using the range() function to generate a temporary list of numbers 0 to 50 for filtering.
We'll be using our filter() operator to filter only even numbers into a new list. Let's go ahead and see the code for this filter:
Here we can see that our filter() operator and our lambda expressions worked! Inside of our lambda expression, we test to see if the remainder of dividing the element by 2 is zero. If this is true, the number is even and passed into the new list.
Let's move on to our next lambda operator, map().
We can use the map operator when we want to apply something to all elements of a list. For this example, we'll be generating a list of numbers 0 to 50 (again using the range() function). Then we'll be multiplying every element in the list times 2. So we'll have a newly mapped list of numbers 0 to 100, with evens only. Let's see this in action:
It worked! We can use map() to apply something to every element in a list. This can be useful for performing basic algebra, where changes need to made all around for validity. The way this works is that it takes the expression, in this case x * 2, and applies it to every element in the list.
Now that we've covered map(), let's move on to our next operator, reduce().
The reduce operator is slightly more confusing. It iterates through a list and performs an action sequentially. For instance (and this will be our example) we can use it to add all of the contents together. For the sake of better understanding, we'll declare our own list instead of using range() for this one. Let's go ahead and make our list:
Now that we have our list, let's reduce it. We'll be using addition in this reduction operation. What this means is that it'll add them together sequentially. It'll start with 0 plus 1, the result it one, so it will then perform 1 plus 2, which is three and leads to 3 plus 4. It will repeat this until the entire list has been reduced to one number. Let's perform our reduction operation:
We've successfully reduced our list using addition. We had to define two arguments for our lambda expression (x and y), one for each part of the addition.
It's very important to understand the sequential part of reduction, as this can impact the result with different operations.
Now that we've covered all this, let's wrap up, shall we?
We've covered quite a bit here, and some of it was pretty confusing. I hope the concepts and syntax got across well. We covered the basics of lambda expressions, and then we went in detail with each lambda operator!
Today we've added even more to our advanced scripting skills. We can use what we've learned here to condense code to make it smaller and easier to read. We can also use this to reduce the total file size of scripts we make. The only tricky part is remembering and finding a place to use these newly found skills.
Have any questions? Message me about them or leave them here in the comments! I'll try my hardest to answer them. Recently we've been covering some of the more advanced topics, so I apologize if the difficulty of these articles has spiked. I'll see you for your python's next training session.
Thank you for reading!