Lambda Expressions (Functions)
Lambda Expressions
- An expression that evaluates to a function
- Known as anonymous functions
- We use lambda functions when we require a (nameless) function for a short period of time.
- We generally use it as an argument to a higher-order function (soon)
- Lambda functions are used along with built-in functions like filter() and map() (soon)
- Used with apply when working with Pandas
- No “return” keyword
- return: Must be a single expression:
- The lambda’s body is similar to what we’d put in a def body’s return statement.
- Can not contain complex statements (like while or for loops)
Lambda expressions must use
else
str.join(list)
Joins a list with the given string
Iterator, Map(), Filter()
Map()
map(function_to_apply, list_of_inputs)
function_to_apply
: can be a lambda functionlist_of_inputs
: iterable
Returns an iterable
Iterator object
- Think of it as a cursor that goes along the list
next()
moves the cursor
Lazy Evaluation (used in iterator objects)
Useful when you have a very large data set to compute. It allows you to start using the data immediately, while the whole data set is being computed.