• A function is a group of related statements that perform a specific task.
  • Functions help break programs into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.
  • Furthermore, it avoids repetition and makes code reusable.

Types of Functions

Basically, we can divide functions into the three types:

  1. Built-in function: Functions that are built into Python.
  2. User-defined function: Functions defined by the users themselves.
  3. Lambda function: Function that is defined without a name.

Anonymous/Lambda Function

  • In Python, an anonymous function is a function that is defined without a name.
  • While normal functions are defined using the def keyword, in Python, anonymous functions are defined using the lambda keyword.
  • Hence, anonymous functions are also called lambda functions.

Syntax:

lambda arguments: expression

Notes :

  • Lambda functions can have any number of arguments but only one expression.
  • The expression is evaluated and returned.
  • Lambda functions can be used wherever function objects are required.

Example 1 : Double the Number

double = lambda x: x * 2
print(double(5))

[out: ]
10

Example 2 : Addition of two numbers

sum = lambda var1, var2 : var1 + var2
print (sum(5,6))  # call function from here

[out: ]
11
sum = (lambda var1, var2 : var1 + var2)(5,6)  # can call function from outside the bracket
print (sum)

[out: ]
11

filter() function

  • The filter() function takes a function and  sets, lists, tuples, or containers of any iterators as arguments.
  • The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.
Syntax:

filter(function, iterable)

Example 1: Using User-Defined Function

marks = [10,20,30,55,80,90]

def fun1(arg1):
    if arg1>50:
        print (arg1)
        
pass_students = filter(fun1,marks)

for i in pass_students:
    print (i)

[out: ]
55
80
90

Example 2 : Using Lambda Function 

my_list = [1,5,4,68,11,3,12]
new_list = list (filter(lambda x:(x%2==0),my_list))
print (new_list)

[out: ]
[4, 68, 12]

Example 3 : Supplanting function with ‘None’

With filter function is None, the function defaults to Identity function, and each element in random_list is checked if it’s true or not.

marks = ['False',True,0,10,False]

pass_students = list(filter(None,marks))

pass_students

[out: ]
['False', True, 10]

map() function

  • The map() function takes a function and multiple iterable such as list, tuple, etc. as the argument.
  • The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.
Syntax: 

map(function, iterable1, iterable2, ...)

Example 1 : Iterating all elements and returns results

marks = [10,20,30,55,80,90]

def fun1(arg1):
    if arg1>50:
        return (arg1)
        
pass_students = list(map(fun1,marks))
print (pass_students)

[out: ]
[None, None, None, 55, 80, 90]

Example 2: Iterating through all elements

my_list = [1,5,4,68,11,3,12]
new_list = list (map(lambda x:(x%2==0),my_list))
print (new_list)

[out: ]
[False, False, True, True, False, False, True]

Example 3 : Iterating all elements

numbers = (1, 2, 3, 4,0)
result = map(lambda x,y: x*y, numbers,numbers)
print(list(result))

[out: ]
[1, 4, 9, 16, 0]

Example 4 : Using multiple iterables

a = (5,6,7)
b = (5,6,7)

new_lst = set(map(lambda a,b : a + b,a,b))
print (new_lst)

[out: ]
{10, 12, 14}

Example 5 : Listify the list of strings individually

l = ['parsis', 'pratik', 'sakshi'] # bool or integer value is not supported
  

test = list(map(tuple,l)) 
print(test)

[out: ]
[('p', 'a', 'r', 's', 'i', 's'), ('p', 'r', 'a', 't', 'i', 'k'), ('s', 'a', 'k', 's', 'h', 'i')]

Difference between filter() and map()

filter()map()
can use only single iterablecan use multiple iterables
applied to only those objects of iterable who goes True on the condition specified in expressionapplied to all objects of iterables irrespecitve result on the condition specified in expression
Take None as functionDoes not takes None as function

Find the code on GITHUB:

https://github.com/parsispresswala/Prasik-s-Blogs/blob/master/Basic%20Python/17%20-%20Lambda%20Function.ipynb