Lambda
Perhaps you want to create a function that takes a function and an argument.
Something that looks like this
Code:
Output:
Here's it is written with the lambda function
Code:
Output:
Lambda is an in-built python function that can also be called an anonymous function that can be written in a single line for a particular function, though not as powerful as using "def" to create a function.
It is written in a format
(Lambda x: 2*x )(15)
Where it takes an argument x and multiplies it by 2 the second parentheses is the argument it takes in
Therefore printing this will result in 30
Code:
Output:
It can also be written in the format
Code:
Output:
Map and filter
These are inbuilt functions in python,
The map is used to pass a function over an iterable I.e a list
Code:
Output:
It takes in two arguments, the first is a function and the second is iterable.
The map function has to be explicitly converted into a list for the value to be seen.
Filter as the name implies is used to filter or remove items on the list based on certain conditions that are specified.
It takes in two arguments the first is the specified condition and the list
Consider the code
Code:
Output:
This is used when you need only certain elements in a list that specifies a condition.
Hope you enjoyed reading this
Like, share, and comment.
Top comments (0)