Map Function in Python:
map() function allow us to "map" a function to an iterable object. which means that we can quickly call the same function every item in an iterable, such as a list, tuple ..
To get the results, we either have to iterate through map() or just cast it to a list.
def sq_of_num(num):
return num**2
my_nums = [1,2,3,4,5]
list(map(sq_of_num,my_nums)) # [1, 4, 9, 16, 25]
names = ['John','Cindy','Sarah','Kelly','Mike']
def validateEvenLen(mystr):
if len(mystr) % 2 == 0:
return 'even'
else:
return mystr[0]
list(map(validateEvenLen, names)) # ['even', 'C', 'S', 'K', 'even']
Filter Function in Python:
filter() function returns an iterator yielding those items of iterable for which function(item) is true. Meaning we need to filter by a function that returns either True or False. Then passing that into filter (along with our iterable) and we will get back only the results that would return True when passed to the function.
def validate_even(num):
return num % 2 ==0
my_nums= = [0,1,2,3,4,5,6,7,8,9,10]
list(filter(validate_even,nums)) # [0, 2, 4, 6, 8, 10]
Lambda Expressions in Python:
- lambda expressions allow us to create "anonymous" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.
- lambda's body is a single expression, not a block of statements.
- The lambda's body is similar to what we would put in a def body's return statement. We simply type the result as an expression instead of explicitly returning it. Because it is limited to an expression, a lambda is less general that a def. We can only squeeze design, to limit program nesting. lambda is designed for coding simple functions, and def handles the larger tasks.
-
Steps to write a lambda funciton from a function:
- delete the def keyword
- delete the function name and enter "lambda"
- the parameters without bracket
- write the return statement after semi-column
- delete the return keyword
## Function Declaration using def:
def square(num):
result = num**2
return result
# It can be written as:
def square(num):
return num**2
# We could actually even write this all on one line.:
def square(num): return num**2
This is the form a function that a lambda expression intends to replicate. A lambda expression can then be written as:.
square = lambda num: num ** 2
square(3) # 9
We wouldn't usually assign a name to a lambda expression
Many function calls need a function passed in, such as map and filter. Often you only need to use the function you are passing in once, so instead of formally defining it, you just use the lambda expression.
list(map(lambda num: num ** 2, my_nums)) #[1, 4, 9, 16, 25]
list(filter(lambda n: n % 2 == 0,nums)) # [0, 2, 4, 6, 8, 10]
names = ['John','Cindy','Sarah','Kelly','Mike']
def validateEvenLengthOfString(mystr):
if len(mystr) %2 ==0:
return 'even'
else:
return 'Odd'
# Above funciton - "validateEvenLengthOfString" can be written in python
validate_EvenOrOddLen_String = lambda mystr: 'Even' if len(mystr) %2 ==0 else "Odd"
list(map(validate_EvenOrOddLen_String,names))
# outupt: ['Even', 'Odd', 'Odd', 'Odd', 'Even']
names = ['John','Cindy','Sarah','Kelly','Mike']
list(filter(lambda name: len(name)%2==0, names))
#output: ['John', 'Mike']
Lambda expression for reversing a string:
lambda s: s[::-1]
Lambda expression for grabbing the first character of a string: :
lambda s: s[0]
We can pass multiple arguments into a lambda expression.
lambda x,y : x + y
We need to keep in mind that not every function can be translated into a lambda expression.Sometimes its easier (and often the only way) to create the def keyword function.