Created
March 19, 2019 16:07
-
-
Save aqd14/0977d1675153c8dc03def7c9f8e092f0 to your computer and use it in GitHub Desktop.
Examples on **lambda**, **map**, and **filter**
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Lambda | |
Syntax: ```lambda arguments: expression``` | |
__Example__ | |
``` | |
# Regular way of creating a function | |
def add(x, y): | |
return x+y | |
add(3, 4) # return 7 | |
# We can shorten function creation by using lambda | |
# lambda will return a function object | |
add = lambda x, y: x + y | |
add(3, 4) # return 7 | |
``` | |
### Map | |
Syntax: ```map(function_object, iterable1, iterable2, ...)``` | |
__Example__ | |
``` | |
def square(x): | |
return x*x | |
list(map(square, [1,2,3,4])) # return [1, 4, 9, 16] | |
# We can shorten the procedure by using lambda | |
list(map(lambda x: x*x, [1,2,3,4])) | |
``` | |
### Filter | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment