Hello guys, welcome to my blog. Here I am going to tell you about the map, lambda, and sets in python.
MAP FUNCTION IN PYTHON:
When working with iterables in the python-like lists, we use for loops to apply the operations on elements of the iterables, but we know that for loops in the python are quite slow. Don't worry, for this, we have MAP function in the python. Its syntax is
map(fun,iterable)
It creates a map object. The 1st argument in the map function is the operation we do on the elements of the iterable. The 2nd argument in it is iterable on which you perform the operation. The map function returns the list containing the elements on which the operation is done. Look at the below example:
l1=[1,2,3]
def func(x):
return 2*x
l=list(map(func,l1))
print(l1)
Output:
[2,4,6]
In the above example, the function func accepts the element and returns the double of the element. To perform this operation on an iterable we loop through it. But here we used map function which performs the func operation on every element in the list and creates an object. Here, we used list(), constructor, it turns the map object into a list, which contains the elements which are the return values of func.
LAMBDA FUNCTION IN PYTHON:
Lambda expressions in Python are similar to functions in python. Its syntax is
lambda arguments: expression
It can have any number of arguments, but it has only one expression. See the below example.
A=lambda x,y: x+y
print(A(2,5))
In the above example, the lambda function has two arguments and returns the sum of two arguments.
So, the output of the above code is 7.
WHY USE LAMBDA FUNCTIONS:
For, this you should see the below example,
l1=[1,2,3]
l=list(map(lambda x: 2*x,l1))
Output:
[2,4,6]
I think your doubt is cleared now.
SETS IN PYTHON:
Now, let us see the sets in python. A set in Python is an unordered collection of elements of any data type and it has no duplicate elements. It takes iterable as its argument. Note that each and every element in the set must be an immutable object. It takes the iterable as its argument. See the examples
s=set() #it creates an empty set , here if you print s, the output will be set()
s=set([1,2,3]) #takes the list as it's argument
s={1,2,3} #another type of declaration of sets
print(s)
Output:
{1,2,3}
Note that the output is the same for both the type of declarations.
l=[1,2,3,4,3,4]
s=set(l)
print(s)
Output:
{1,2,3,4}
As a set should not allow duplicate elements into it, the output will be the set of unique elements.
A set element should always be an immutable object. See the below example,
set([1,2,[1,2]]) #if you try to execute this, an error will occur
set([1,2,(2,3)]) #this will run successfully because the tuple is also an immutable object.
There are so many methods are there in sets. To know about them, visit: https://docs.python.org/2/library/sets.html
Now, I will explain the code of Sock Merchant in Hackerrank
n=int(input())
l=list(map(int,input().split()))
l1=set(l)
f=l.count
l2=list(map(lambda x: f(x)//2,l1))
print(sum(l2))
Here, the first line will the number of socks. The second line will take the n space-separated integers describing the colors of the socks in the pile.
In the 2nd line, the arguments in the map are int and input().split() as the input entered by the user is delimited by space we use .split() function which returns the list of elements of input of type string. The map function maps this list to int which converts the elements in the list to data type int and returns the list of elements of type int.
In the third line, the set function returns the set of elements into the l1 variable without duplicate elements.
In the 4th line, I assigned the method of list .count() f to make the code to run faster, as we are calling this method in the 5th line, many no. of times.
In the 5th line, we are using map and lambda functions instead of a loop, and we optimized our code. Now, l2 contains the elements which are the no. of pairs of each color of sock.
In the last line, we just printed out the total no. of pairs.
THANKYOU... HOPE YOU HAVE ENJOYED MY TUTORIAL. FOR MORE, SUBSCRIBE TO MY BLOG AND GET THE EMAIL NOTIFICATIONS FOR THE LATEST POSTS.
AND ALSO SUBSCRIBE TO MY YOUTUBE CHANNEL: https://www.youtube.com/channel/UCPnw9O8cvhX9mS195imI8Sw
FOLLOW ME ON GITHUB: https://github.com/VallamkondaNeelima/MyCodes/tree/master
Comments
Post a Comment