Python Map

I am Sravya in this article I’m going to cover lambda functions map functions filter functions and reduce functions in Python 3, now these are the new functions added to Python 3, they are also added to Java 8 the newest version of java in case you are using java.

These are really borrowed from functional programming languages like lisp.

Lambda function:

Lambda function is a simple one-line function and it does not use DEF and return keywords like traditional python functions. These are implicit in the function so the whole point of a lambda function is to make it very short simple and quick and easy to implement.

Let’s take example simple lambda function let’s say we want to double the value we could write a traditional python function called def double X and we can return X times 2.

#Double X

Def double(X):

Return X*2

Lambda function would look like this we use the lambda keyword and then we have our parameter in this case there’s one parameter it’s X so that’s our variable name for the parameter we must have the colon and then the return value and the return value can be a function applied to that variable where here we have 2 times X as our return so that’s a very simple lambda function it just doubles the value you pass.

Lambda X:2*X

let’s say we want to have two variables that are passed in we want to add x and y together so a traditional function in Python would look like def add X comma Y return X plus y. A lambda function again we take in two variables X comma Y we still need this colon here and then this is our return x plus y so we’re returning the sum of x and y and this is a simple lambda function it does this in one line.

#add x and y

Def add(x,y):

Return x+y

Lambda x,y:x+y

So if we want to take the max of x and y here our traditional function gets a little bit longer because we’ve got an if-else statement so we call DEF MX of x comma y if X is greater than Y return X else return Y obviously you could write this in a shorter syntax in Python if you wanted to but this is a standard way and then print max of 8 comma 5.

#max of x,y

Def mx(x,y):

If X>Y:

Return x

Else:

Return Y

Print(MX(8,5))

So our lambda function in python this time we are going to assign this to a variable MX where MX is actually the lambda function itself and we have the lambda keyword and then X comma Y or the two variables we’re going to pass in and what we’re going to return comes after the : X if X is greater than Y else Y so we have an if-else statement here and this is the syntax for applying an if-else statement in the returns part of the lambda function.

Mx = lambda x,y:x if x>y else y

Print(mx(8,5))

Map Function:

Apply the same function to element of sequence or normally list and then return the modified list.

Let’s say the black box in the middle here is our map function and we want to pass list of items m,n,p in a function let’s say function f and then the new list that we get out of this map function is we applied the function to M, we applied the function to n we applied the function to P and we got a list of items so that’s what the map function does it applies this function to each item in our list and returns a new list.

so let’s say we have a list of items 4 3 2 1 and we want to square each of those items we could define a standard Python function to square each item we pass in a list list 1 we create an empty list list 2 and for each number in list 1 we append the square of the number 2 list and then we return list 2 so that would return 16 9 4 and 1 and then this print statement would print it out so that’s a standard way to do it in Python.

#print [16,9,4,1]

Def square (lst1):

Lst2 = []

For num in lst1:

Lst2.append(num**2)

Return lst2

Print square([4,3,2,1])

By using map function we could do a display we’ve defined in as the list 4 3 2 1 and then we call the map function we pass into that both a function which in this case we can use a lambda function and a list we just defined here so the lambda function is going to receive a variable X and then it’s going to return x squared so the map function is going to iterate through list in and apply this lambda function to each item in this list and each item is going to be passed into the lambda function as a variable named X and then we’ll explicitly cast the return value as a list and print it out alternatively.

n = [1,2,3,4]

print(list(map(lambda(x:X**2,n)))

We don’t necessarily have to use a lambda function for the map function so here we use a lambda function as our function but here we decided we can use square function that we defined over here so we pass in the square function we pass in the same list in that we have up here and what we get back is this same list 16 9 4 and 1 then we have to explicitly cast that as a list and then we can print it so this basically does exactly the same thing using this function instead of this lambda function.

Print(list(map(square,n)))

you’re familiar with list comprehensions I’ll make a separate article on this I hate to hijack this article but these comprehensions are really handy for simple stuff like this because you can do with a pretty minimal amount of code you can do basically the same work so if I wanted to print x squared for X in n could just do that with a simple list comprehension and a print statement.

Print([X**2 for x in n])

Filter:

The filter function filters items out of a sequence and it will return the filtered list so let’s see we have a black box that we want to pass in some stuff too this is our filter and we’re going to pass in both a list of items M,N and P and a condition C that we’re going to apply to each item in what we get out of it is a new list of items to actually satisfy the condition.

If we have a list of items 4 3 2 1 again and we just want to print the ones that are over 2 so 4 & 3 we could define a simple traditional Python function and we’ll say list 2 equals here I used a list comprehension in traditional Python function X 4x and list 1 if X is greater than 2 so for any values in list 1 there are over 2 it will add those to list and will return list 2 and print out the result which will be 4 and 3.

#Prints[4,3]

Def over_two (lst1):

Lst2 = [x for x in lst1 if x>2]

Returns lst2

Print over_two([4,3,2,1])

Our filter function over here we’re going to pass in a condition and a list the condition again we used a lambda function for the condition the lambda function receives this variable X and it will return values where X is greater than 2 and then this is our list so we pass in a condition and a list in our condition is lambda function is underlined the filter will apply this condition to each item in the list and will return a new list and we have to explicitly cast the return value as a list in Python and then we can print it and again .

n = [4,3,2,1]

print(list(filter(lambda x:x>2,n)))

We can use a simple list comprehension this is almost identical to this I have in my traditional function if we just want to filter the items in this list that are greater than 2 we can use a simple disk comprehension to do that so that’s an alternative way of doing it.

Print([x for x in n if x>2])

Reduce:

The reduced function applies the same operation to items of the sequence to each item in the sequence and it uses the result of each operation as the first parameter of the next operation so if you have let’s say a list of items m,n and P and you want to pass in a function f where f takes two parameters it will first take parameters m and n out of this list and it will apply F to it and then it will use the result of that combined with P so what we’re doing is the reduce function is going to take F of M and N and then it’s going to take the result of that as the first value and P is the second value.

So a traditional Python function would look something like this def mult list 1 and then we have a product called prod that equals the first item in list one and then we’re going to iterate through the list we’re going to multiply prod by that the I’th item in the list and then we’ll return the final product so look at 24.

# Prints 24

Def mult (lst1):

Prod =lst1[0]

For I in range(1,len(lst1)):

Prod*=lst1[i]

Return prod

Print mult([4,3,2,1])

Back using the reduced function we define our list in four three two one we pass in a lambda function lambda x and y that takes two variables x and y and we return the product those x times y and this is the list that we pass in so this reduce function will apply this lambda function to each pair of items in the list so it starts with 4 & 3 then it gets 12 it uses the 12 as the first item in the second iteration so it’s going to multiply the product of 4 and 3 by 2 to get 24 then it’s going to multiply the product of that by 1 to get 24 again so it’s going to return 24 so that’s how the reduce function.

N = [4,3,2,1]

Print (reduce(lambda x,y:x*y,n))

4*3 = 12

12*2 =21

24*1 = 24

Leave a Reply

Your email address will not be published. Required fields are marked *