Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.6: Lambda and Map Functions

2 Replies, 1288 Views

[Image: BFMMlbcQvml9HSqXcvNp]

Lambda/Anonymous functions:
Lambdas are actually single line functions with no names that’s why these functions are also called anonymous functions. We use lambda functions when we want to get rid of the overall syntax of defining a function and that function is of a single line. So it makes our code readable and small.
Let’s suppose we want to make a function to get the average of three numbers so what we would do


Code:
def avgFunc(a,b,c):
    return (a+b+c)/3
print(avgFunc(10,20,30))


So this is what we will do but we can do it so simply using lambda functions


Code:
avg = lambda a,b,c: (a+b+c)/3
print (avg(10,20,30))


So which one is more simple we use lambda keyword for defining lambda function and it has no name after lambda we just pass arguments and colon (Smile and after colon the function statement. So simple isn’t it? The concept of lambda is just that it is a way of defining a function.

Map function:
So now we jump into another concept that is map filter concept. In this part we will see its use and why it is important. So without wasting any time let’s jump into it. Maps are mostly used when we want to apply a function on all element of list lets take an example from old methods


Code:
mylist = ['154','241','34','123']
for i in range(len(mylist)):
    mylist[i] = int(mylist[i])
mylist[2] = mylist[2] - 10
print(mylist[2])


Quote:Output: 24


In this example we have a list of numbers that are string data type but we have to perform some arithmetic operation on them so that we convert each element into integer data type. But we can use map function for that to make the code more readable


Code:
mylist = ['154','241','34','123']
mylist = list(map(int , mylist))
mylist[2] = mylist[2] - 10
print(mylist[2])


Quote:Output: 24


We should convert it into a list type because by default map returns an object so we convert it into a list and then perform our operation on it. Let’s code another example which will make it clear to understand in depth.


Code:
mylist = [1,2,3,4,5]
mylist = list(map(lambda x:x*x , mylist))
print(mylist)

Quote:Output: [1, 4, 9, 16, 25]

Map takes two arguments: the first one should be the function that you wanna apply on list items and second the items of list. So here we are passing lambda function as argument to map function.


Code:
# Program to print cube of first ten numbers
mylist = [lambda x:x*x , lambda x: x*x*x]

for i in range(1 , 11):
    val = list(map(lambda x:x(i), mylist))
    print(val)


Quote:Output:

[1, 1]   
[4, 8]
[9, 27]
[16, 64]
[25, 125]
[36, 216]
[49, 343]
[64, 512]
[81, 729]
[100, 1000]

So this is a lil bit advanced program but i’ll try to make it easy for you and tell you each and everything in the code.
In the first line we create a list that has two elements: first one square of any number and second cube of any number.

Then we make a loop to traverse the list then at each spot we pass a number using map function. At first the value of i is 1 so the square and cube both are 1
In the second iteration i is two it will pass two lists and for the first item two is passed to first lambda function which makes it 4 and second one makes it 8. Similar in each step and so on.
(Continued...)

Quote:in next thread we will cover filter function and some other functions. Till than 


"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
"Learn for a better future"
Love me like you do Heart
(This post was last modified: 05-14-2022, 09:02 PM by Covid-19.)
Amazing, The lambda been always a helpful function...

About the map thing still a little bit complicate wish you could print the output as well.
Rs
* Thankful to Allah *
Kurdy
(05-14-2022, 08:41 PM)Mr.Kurd Wrote: Amazing, The lambda been always a helpful function...

About the map thing still a little bit complicate wish you could print the output as well.

Thanks for your appreciation. I have just edited the thread and add outputs for map code if it has still any issue then let me know.
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,606 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,644 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,823 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,589 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,822 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,771 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,800 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,803 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,798 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)