Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Skills:
LLM Foundations60%
Key Takeaways
This video tutorial covers the use of lambda functions in Python, including their syntax and use cases with built-in functions such as sorted, map, filter, and reduce.
Full Transcript
hi everybody welcome to a new python tutorial today we're gonna talk about lambda functions in Python a lambda function is a small one line and Animus function that is defined without a name and it looks like this first it has the lambda keyword then it can take some arguments then a colon and then an expression and what this will do this will create a function with some arguments and it evaluates the expression and returns the result so let's look at an example to make this clearer let's call a function and we call this at 10 and this is equal a lambda with an input and let's call our input X and then it should evaluate X plus 10 so this will create a function with one argument and it adds 10 to the argument and returns the result and we assign this function to our variable at 10 so now this is a function that we can call with an argument so let's call it with with 5 and now if we print this then it will print 15 so this is practically the same as a normal function like this let's call this at 10 func and this will take an argument X and return x plus 10 so these two things do the same thing but the lambda function is much shorter and only in one line so lambda functions can also have multiple arguments so let's say let's create another lambda function and call this mouth and and this is equal lambda and now we give it x and y and it should evaluate x times y so this will create a function with two arguments and it will multiply these two arguments and returns the result so now if we print for example mouth two and seven then it will print 14 so that's the lambda syntax lambda functions are typically used when you need a simple function that is used only once in your code or it is used as an argument to higher-order functions meaning functions that take in other functions as arguments for example they are used along with the built-in functions sorted map filter and reduce and we will have a look at all of them to make the usage of lambda akhira so let's start with the sergeant method so you probably already know this and I also show this in my video about lists so let's say we have a list and we call our list points to D and the list has two posts with two elements in it so you can think of this as the X and the y values of our points and now if we want to sort this so let's create a point to D sorted list and then we can call sorted this is built-in so we don't need to import anything and now we can sort our list so we want to sort points to D and now print first print our points and then print our points to D sort it so by default this will sort our as our list by the first argument so by the x argument so 1 5 10 and 15 but we can also give it a specific rule how to sort it so we can say we can give it a key argument and the key equals and this should be a function and as we now know we can write a function with a lambda in one line so we can say lambda with an argument X and now let's say we want to sort it by the Y value so by the second index so then we say X of the index 1 so now if we run this then we will see that our list got sorted according to the Y index so what this does is you can also for example give it a or define a function and let's say sort by Y and then give it a index and in this give it an argument and in this case the argument is a tuple and then it returns the first index so now we can also use this function here so sort by Y and if we run this then this will return the same result but now we see with a lambda we don't need this and then we can simply use our lambda here so we can use our lambda here and get rid of this function and yeah that's one use case of a lambda for example let's make another example of sorting let's sort this according to the sum of each so therefore we would say lambda X and then evaluate X of index 0 plus X of index 1 now if we run this and we see that it got sorted according to the sums of each tuple so that's the sorted method with a lambda is key argument now let's talk about the map function so the map function transforms each element with a function so it looks like this it has a func a function as an argument and then a sequence so this is for example a list so let's create a list with some numbers in it so one two three and four and five and now let's create another list and call this B equals and now we will want to multiply each element by two so let's say map and then as a function we define a lambda with an argument and evaluate x times two and then as a second argument when you use our list and then we print this and if we want to print this and if we simply print it like this then it will print a map object so we have to convert it to a list first and then we can see that each element got multiplied by two so that's the map function however you can achieve the same thing with list comprehension so you probably already know the list comprehension syntax it's a little bit easier so you can write it like this C equals and then let's say x times two for X in a now if you print this then this will do the same thing so personally I would prefer this syntax it's a little bit easier but you should have heard about the map function now the second function is the filter function so the filter function also gets a function and a sequence and it will this function must return true or false and the filter function will return all elements for which the function evaluates to true so let's say let's also give it a 6 and let's say we want to filter this and we say B equals filter and let's say in this example we only want to have the even numbers so then we create a lambda with X and we evaluate X modulo 2 equals equals 0 and then if we run this we should get only the even numbers so again here we can achieve the same thing with list comprehensions so we can also write C equals a list and then inside our list we write X for X in a and then we can give it a condition we can say if X modulo 2 equals equals 0 so if you print C then we see that this will do the same thing and as a last function I want to show you the reduced function so the reduced function also takes a function and a sequence and it repeatedly applies the function to the elements and returns a single value so let's say I have a list here and I want to compute the product of all the elements so let's call this prod product a equals and then I can say read use and in Python 3 I have to import this now so I have to say from func tools import Jews and then I can come call the reduce function and as a first argument I give it a function so I define the function here again in one line with a lambda I say lambda X and now it has two arguments here so function the function for the reduced function always has two functions has two arguments so let's say x and y and then it should evaluate x times y and as a sequence I give it a so let's print the product so this will print let's make this for example smaller then we can see it has one times two equals two two times three equals six and six times four equals 24 so yeah that's the reduced function and that's all I wanted to show you about lambdas I hope you enjoyed this tutorial and see you in the next tutorial where we talk about exceptions in Python
Original Description
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
In this Python Advanced Tutorial, we will be learning about Lambda functions in Python. A lambda function is a small (one line) anonymous function that is defined without a name. It is typically used when you need a simple function that is used only once in your code, or it is used as an argument to higher-order functions. We will go over how you define them, and some use cases together with the built-in sorted(), map(), filter,() and reduce() functions.
~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~
✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer *
📚 Get my FREE NumPy Handbook:
https://www.python-engineer.com/numpybook
📓 Notebooks available on Patreon:
https://www.patreon.com/patrickloeber
⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN
A written Tutorial can be found here:
https://www.python-engineer.com/courses/advancedpython/08-lambda/
You can find me here:
Website: https://www.python-engineer.com
Twitter: https://twitter.com/patloeber
GitHub: https://github.com/patrickloeber
#Python
----------------------------------------------------------------------------------------------------------
* This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Patrick Loeber · Patrick Loeber · 8 of 60
1
2
3
4
5
6
7
▶
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
PyTorch Tutorial 01 - Installation
Patrick Loeber
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI