User Defined Functions V
User Defined Functions V
• The first argument func is the name of a function and the second argument,
iterable, should be a sequence (e.g. a list, tuple, string etc) or anything that
can be used with for loop.
• map() applies the function func to all the elements of the sequence iterable
What Is map() Function ?
• Now suppose we want to call this function for the following list of
numbers:
• mynums = [1, 2, 3, 4, 5]
• One way to do this, will be to use for loop
mynums = [1, 2, 3, 4, 5]
for x in mynums:
print(square(x))
Complete Code
def square(num):
return num**2
mynums = [1, 2, 3, 4, 5]
for x in mynums:
print(square(x))
Output:
1
4
9
16
25
Using map() Function
To make it even shorter we can directly pass the list() function to the function
print()
def square(num):
return num**2
mynums = [1, 2, 3, 4, 5]
print(list(map(square, mynums)))
Output:
[1, 4, 9, 16, 25]
Previous Code Using map()
In case we want to iterate over this list, then we can use for loop
def square(num):
return num**2
mynums = [1, 2, 3, 4, 5]
for x in map(square, mynums):
print(x)
Output:
1
4
9
16
25
Exercise
• Now the function filter() applies the function passed as argument to every
item of the sequence passed as second argument.
• If the function returned True for that item, filter() returns that item as part of
it’s return value otherwise the item is not returned.
What Is filter() Function ?
• Another way to solve the previous problem is to use the filter() function.
• The filter() function will accept 2 arguments from us.
• The first argument will be the name of the function check_even
• The second argument will be the list mynums.
• It will then apply the function check_even on every element of mynum and if
check_even returned True for that element then filter() will return that
element as a part of it’s return value otherwise that element will not be
returned
Previous Code Using filter()
def check_even(num):
return num % 2 == 0
mynums = [1, 2, 3, 4, 5, 6]
print(filter(check_even, mynums))
Output:
<filter object at 0x00000000029F3F60>
• As we can observe, the return value of filter() function is a filter object
• To convert it into actual numbers we can pass it to the function list()
Previous Code Using filter()
def check_even(num):
return num % 2 == 0
mynums = [1, 2, 3, 4, 5, 6]
print(list(filter(check_even, mynums)))
Output:
[2, 4, 6]
Previous Code Using filter()
In case we want to iterate over this list, then we can use for loop as shown below:
def check_even(num):
return num % 2 == 0
mynums = [1, 2, 3, 4, 5, 6]
for x in filter(check_even, mynums):
print(x)
Output:
2
4
6
Guess The Output
print(list(filter(f1, mynums)))
Output:
TypeError: f1() takes 0 positional arguments but 1 was given
Guess The Output
def f1():
pass
mynums = [ ]
print(list(filter(f1, mynums)))
Output:
[]
Guess The Output
def f1(num):
Since f1() is not returning anything,
pass so it’s return value by default is
assumed to be None and because
mynums = [1, 2, 3, 4, 5] map() has internally called f1() 5
times, so the list returned contains
print(list(map(f1, mynums))) None 5 times
Output:
[None, None, None, None, None]
Guess The Output
def f1():
pass
mynums = [ ]
print(list(map(f1, mynums)))
Output:
[]
Using Lambda Expression With map() And filter()
• So, we can pass this lambda expression as first argument to map() and
filter() functions, since their first argument is the a function object reference
• In this way, we wouldn’t be required to specially create a separate function
using the keyword ef.