Do more Write Less is one of the features of python. Python allows us to perform tasks and jobs with less lines of code. Lambda functions are one of the ways to achieve this.
Let us Dive into Lambda Functions.
What is Lambda Function?
A quicker way to write functions in python is termed as Lambda Function. They can also be called as anonymous functions. Keyword Lambda is used to write such functions.
Syntax for Lambda functions is
ย lambda arguments : expression
From syntax we can identify that there is no function name hence the name Anonymous function. Arguments refer to parameters and expression is the function body.
Consider example
A normal function to calculate square of given number
def square(number):
result = number * number
return result
print(square(4))
Output:
16
Same function can be written as lambda function as
square = lambda number : number*number
print(square(4))
Output:
16
Lambda function to calculate power.
x -> first argument y -> second argument x**y -> return valueย
power = lambda x, y : x**y
print(power(4,3))
Output:
64
What is map() function?
The map function takes two parameters viz. a function and a sequence like list. map makes an iterator that applies the given function to every element in sequence.
We can pass lambda function to map function without even naming it. Here, we refer to the lambda function as an anonymous function.
Example:
Multiply every element in list by 2
listA = [1,2,3,4,5]
double = map(lambda num : num * 2, listA)
print(list(double))
Output:
[2,4,6,8,10]
Here we cast double to list to get result as double is map object
What is filter() function?
Filter function filters or removes elements from a list based on criteria.
It takes two parameters: a function or None and a sequence.
Let us see working of filter()
listB = [False,True,True,False,True,False]
print(list(filter(None,listB)))
Output:
[True,True,True]
In the above example we passed a list of random booleans to filter with function None which specifies to return the items that are true.
filter() with lambda
Example:
Remove all strings in given list with length equal to 4
listC = [โoneโ,โtwoโ,โthreeโ,โfourโ,โfiveโ,โsixโ]
filter_list = filter(lambda string : len(string) == 4, listC)
print(list(filter_list))
Output:
[โoneโ,โtwoโ,โthreeโ,โsixโ]
What is zip() function?
We all often come across .zip or zip files. Zip files are files which have zipped other files in them. In simpler terms, zip files are containers to hold other files.
In python, the zip function works more or less like a container for iterables instead of files.
Syntax:
zip(*iterables)
It takes an iterable as input and returns an iterator that aggregates elements in each of the iterable. The output is a tuple of iterators.
If the iterables in the input are of unequal sizes, the output iterator stops when the shortest input iterable is exhausted. If no input is passed it returns an empty iterator.
Example:
Short = [โAMZNโ,โFLKTโ,โEBAYโ]
Company = [โamazon.comโ,โflipkart.comโ,โebay.inโ]
Zip1 = zip(short,company)
For sh,co in zip1:
print(โfull name of {} is {}โ.format(sh,co))
Output:
full name of AMZN is amazon.com
full name of FLKT is flipkart.com
full name of EBAY is ebay.in
print(list(zip1))
Output:
[(โAMZNโ,โamazon.comโ),(โFLKTโ,โflipkart.comโ),(โEBAYโ,โebay.inโ)]
Unzipping:
new_sh,new_co=zip(*zip1)
print(new_sh)
Output:
(โAMZNโ,โFLKTโ,โEBAYโ)
print(new_co)
Output:
(โamazon.comโ,โflipkart.comโ,โebay.inโ)
Practise more problems using the lambda function.
Happy learning!