Hello everybody,
Michael here, and today’s lesson will be on lambdas and list comprehension in Python.
First off, let’s discuss lambdas. Lambdas are small, anonymous functions (meaning nameless functions) in Python that can take any number of arguments/parameters but only one expression. Here’s the general syntax for lambda functions (and in case you didn’t figure it out, lambda functions always start with lambda):
lambda arguments/parameters : expression- Lambda functions are always just one line long!
To help you better understand lambda functions, here’s an example of a simple lambda function:

In this example, the lambda function only has 1 argument-a-and 1 expression-a**2. Simple enough right? However, the way to call a lambda function is different from the way you would call a regular function. Since lambda functions are anonymous, you can’t simply call lambda and insert the parameter(s). You need to store the lambda function in a variable (x in this case), call the variable with a print statement, and use the parameter(s) in the variable call (in this case, x(5) is the variable call.
Now, I said lambda functions can take multiple arguments. Here’s an example of a lambda function with several arguments:
This lambda function has 3 arguments-a, b, and c-and 1 expression-a+b-c. In this example, the parameters for the lambda function are 3, 5, and 7. In lambda functions, Python assigns values to the parameters based on the order that the parameters are listed, so a is 3, b is 5, and c is 7.
Another cool thing about lambda functions is that you can use them inside regular functions. Here’s how that is possible:
In order to use a lambda function inside a regular function, first create a regular function (one that starts with def), then create a lambda function inside the regular function.
- Notice how in this example, the regular function
lambdatesthas a parameter ofywhile the lambda function has a parameter ofz. When you are creating a lambda function inside a regular function, it’s good practice to give the parameters of the lambda function and regular function different names.
However, to call the lambda function inside the regular function, you would first have to create a variable for the lambda function (in this example, the variable is funct). However, the value of the variable would be a call of the regular function. In this example, the value of the variable for my lambda function (funct) is lambdatest(3), which is a call of the regular function lambdatest.
After doing this, there’s one more step. In order to call the lambda function, type (print(lambda function variable(parameters if any))); in this example the line of code you would use is print(funct(6)). After you execute this code, you will get the output of the lambda function-2.
You might be wondering why there are two different function calls with two different sets of parameters. This is because the lambda function-(2**y)-z-takes its parameters from both the lambda function (z) and the regular function (y), so in order to call the lambda function in the regular function, you would have to call both the lambda function (in the form of a variable) and the regular function.
OK, that’s all I have for lambda functions. Now let’s delve into list comprehension.
What is list comprehension? In Python, list comprehension is a simpler and more concise method of creating lists in Python.
Here’s an example of list creation in Python. Looks correct right?:
As you can see, there’s nothing syntactically wrong with this code. A for loop iterates through all of the elements in list2 and adds them to list1 without any errors.
However, even though this code works fine, it’s a cumbersome way to create lists in Python. Here’s how you would create this same list with list comprehension:
Pay attention to the line list1 = [i for i in list2]. This line of code creates list1 by iterating through all of the elements in list2. The for loop in the previous example does the same thing, but this line of code is more concise. Plus, creating a list using for loops takes several lines of code, while list comprehension only takes a single line of code.
-
- Personally, I think list comprehension is another great functionality of Python that I wish Java had (but unfortunately they don’t).
List comprehensions look different depending on the kind of list you’re trying to create, but all list comprehensions in Python follow this basic format:
-
name_of_list = [expression for member in iterable]
What do each of these components mean? Let’s break them down:
-
Expression-this can be any valid expression that returns a value. In the example above,iwould be the expression.- For the example above,
ialone works as an expression, but you can use something more complex for the expression if you wish.
- For the example above,
Member-this represents a value in the list. In this example,iis the member that represents each individual value in the listIterable-this represents the list that the list comprehension function will iterate through. In this example, the iterable would belist2, sincelist1is created by iterating through the elements inlist2one at a time.- List comprehension functions can have an optional fourth element-which I’ll call the conditional-that can filter elements in a list based on a certain set of criteria (I’ll discuss this more later).
Now that I’ve discussed the basics of list comprehension, let’s demonstrate an example of list comprehension using conditional logic:
In this example, list4 is created using the elements in list3 that meet the criteria defined in the if statement-i % 4 == 0. In other words, list4 will contain the elements from list3 that can be divided evenly by 4; only 3 elements from list3 meet that criteria, so list4 only contains 3 elements (12, 24, 60).
As you can see, list comprehension is a pretty versatile functionality in Python. However, you might not have realized that list comprehension can not only iterate through a single list but can also iterate through multiple lists at once to create a super-list. Here’s an example of this:
In this example, the list z is created by iterating through the elements in two lists-[12, 24, 28] and [2,3,4]. However, the creation of list z isn’t as simple as printing out all of the elements of these two lists. Since there is an expression at the beginning of the list comprehension function-x*y-each of the values in the first list will be multiplied by each of the values in the second list and all 9 of the resulting products will be displayed.
Why will there be 9 products? We are multiplying all 3 of the elements in the first list by all 3 of the elements in the second list, and 3*3 is 9.
Thanks for reading,
Michael