Python Lambda Functions Explained
Key Takeaways
This video explains Python Lambda functions, including their use with map, filter, and reduce functions for data processing, and demonstrates how to apply them in various scenarios, such as filtering, sorting, and reducing lists.
Full Transcript
in this video you'll learn about the seemingly complicated Lambda function in just a few minutes now the Lambda function is really just a online Anonymous function it's actually fairly simple it just looks a little bit complicated now let me Define one so you can see exactly how it works so I'm going to make a variable called add one and I'm going to make this equal to a Lambda and then I'm going to have some different parameters for my function now in this case I'll just take a parameter X I'm then going to put a colon and what I put after the colon is an expression that will immediately be returned from this function like I said it's a oneline Anonymous function the anonymous component means that I don't actually need to assign this a name in this case I am I'm storing it in a variable add one but I can use it without declaring it as a variable or associating it with some kind of name now if I want to use this function I can say add one and I can pass the value one to it we could store that in a result variable and we can print that out and you'll see that when we run the code we get the value of two now obviously we can make this a bit more complicated we could accept another parameter like X and Y and now I can do x + y this just means I need to pass another parameter here and if I save my code and run you see that now we get the value 8 Lambda functions can accept any number of parameters and they can return any valid python expression now just to show you the equivalent function if we were to do this without a Lambda I could say Define addore 1 one I could accept my parameters X and Y and then I could return X and Y these two lines are actually the exact same thing and the reason why we would prefer a Lambda function is we would Define this in a place where we don't need this function definition so maybe we're never going to use it again we're just passing this as an argument to a particular function in fact that's where Lambda functions are most useful when you're using them as an argument and you want to define the function in the place where you're using it because it doesn't makes sense to defin it as a standalone function you'll see what I mean now is we go into the examples of looking at map and filter which is where the Lambda function is most useful so let's look at an example here where we utilize the map function now what the map function does is take some list or some iterable object and apply a function to every single value inside of it in this case what we do is we take all of these numbers we pass them to the square function and we generate a new list that contains all of those results or all of those squares so if I run the code you can see that we get all of the squares from 1 to 10 now this is fine and in this case we've used a function that we've defined normally using the deaf keyword however this is really where we would want to use a Lambda function because it's a little bit more elegant and we don't need to use this Square function again it's kind of redundantly defined we're not going to utilize it again like we would a normal function so what I can do is remove that and instead place a Lambda function directly inside of here so I can say Lambda X and then x to the exponent 2 now this works well because this is a very simple function it's one line it's Anonymous meaning I don't need to store a reference to it after we're finished with this and now when I run the code you can see that we get the exact same result so that's really the use case of the Lambda function when you need to pass this as some kind of argument typically to another function now another common use case of the Lambda function is with the filter function now the filter function will simply apply a function to every single value inside of some kind of list or iterable object and if the function returns true it will keep this item if it returns false it will reject it so it's filtering some kind of iterable object for us now in this case I've written a Lambda function this takes in the value X is the parameter it does the modulus two of this and sees if that's equal to zero now really this is just checking if the value is even and if it is it will keep it otherwise it will reject it so when I run the code here you can see now that we get 2 4 68 10 just another very common use where you'll see the Lambda function inside of the filter function as the function that's being used to filter the different elements now another instance in which you'll see this Lambda function is as the key function for some kind of sort now in this example here you can see that we have a list and we have a few different tles inside of here now we have a number we have a letter we have a string and it's kind of ambiguous which element we actually want to sort by or how we should sort these various elements now we can specify exactly how to sort that by passing a key function now in this case what the key function will do is simply take any one of our objects here and just strip out the element at index one which in this case is going to be the letter this now is the element that python will sort based on and it's a very common use case for using this Lambda function so if I run this code here you can see now that we're going to sort based on the middle element so the letter so we have a b and c and that's how the sort was applied now we can obviously change this and we can make it more advanced for example I could say X1 + X2 or something along those lines and then if we had a tie it would end up sorting by the second string so now let's save this and run and you can see that we end up getting 1 2 3 so now let's look at a few more advanced examples with the Lambda function now in order to do that I'm going to introduce a new function to you called reduce now the way the reduce function works is going to reduce down some kind of iterable object to a single value it does that by utilizing a reduce function now this function is going to accept two arguments the first is the accumulated value that we have so far and the second is the next value that we're going to be processing from our iterable object Now by default the accumulated value when we start will simply be the first value inside of the iterable and then X or the value we're processing will be the next one so as we begin and we'll reducing these numbers here 1 2 3 4 5 what's going to happen is the accumulator is going to be one and and X is going to be 2 that means that this function is going to return three now three will become the new accumulated value that's what we have so far and from three we're going to add to that the next value here which is three now that's going to give us six that means the next accumulated value will be six from the six we're going to add the next value which is four that gives us 10 that means the next accumulative value is going to be 10 so we're going to have 10 we're going to add five to that and we're going to get 15 which is eventually what the output will be so we've done that using a Lambda function that has two parameters that's all reduce is doing it's just taking whatever was returned from the last call passing that as the next accumulator and then adding in whatever that additional value is that we're processing now this is a simple example where we're just adding all of the numbers together but we also can use this to find the maximum value in some kind of list which is what we're doing down here now notice what I've done in this example is I've actually written an inline if statement and that's the expr R I'm returning from my Lambda function which is totally valid so I'm saying that I'm going to return the accumulated value if the accumulated value is greater than x otherwise I'll just return X now really all that means is I'm just going to keep the largest value that I have as soon as some value is larger than that then we'll return that one otherwise we'll keep the previous one because it was larger quite simple that's how we get the maximum value using reduce and using the Lambda function and as a quick last example feel free to have a look at this code right here it's completely useless and there's no reason why you would ever write it like this but this is valid python syntax and I figured you could have a look at it all we're doing is defining a Lambda function and then calling it directly in line by having the call symbol after the Lambda function definition again valid syntax I don't know why you would do this but it is something you can do so I figured I'd show it to you that's it that's going to wrap up this video If you enjoyed make sure to leave a like subscribe to the channel and I will see you in the next one he [Music]
Original Description
To learn programming and Python - check out Datacamp!
💻 Learn Python - https://datacamp.pxf.io/daN0E3
💻 Learn Programming - https://datacamp.pxf.io/MmGxWY
In this video, you will learn Lambda functions within Python in just a few minutes. While they may look seemingly complicated, the Lambda function is actually fairly simple as it's just a one line anonymous function and throughout the next 8 minutes, I will show you exactly how they work and how to master them.
Want to make real money with coding? I share high-signal insights on careers, monetization, and leverage in my free newsletter. Join here and get my guide How to Make Money With Coding instantly: https://techwithtim.net/newsletter
⏳ Timestamps ⏳
00:00 | Lambda Functions Explained
02:09 | Map
03:21 | Filter
04:09 | Sort
05:14 | Advanced Examples
Hashtags
#python #lambda #coding
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
Follow-up: The ArxivLens Protocol: Transforming Research Nois
Dev.to AI
On July 1, 2026, arXiv will spin out from Cornell University, its home for the past 25 years, to become an independent nonprofit organization. Major funding support from Simons Foundation and Schmidt Sciences. Ditching the red for their website. [N]
Reddit r/MachineLearning
CS-NRRM™ Official Publications: Paper 1 and Paper 2 Are Now Available
Medium · Data Science
Found a potential mistake in an ICLR 2026 blogpost [D]
Reddit r/MachineLearning
Chapters (5)
| Lambda Functions Explained
2:09
| Map
3:21
| Filter
4:09
| Sort
5:14
| Advanced Examples
🎓
Tutor Explanation
DeepCamp AI