Lambda: Lambda functions are known as the anonymous functions in Python. This kind of functions are declared without any name.
- Can accept any number of argument.
- Restricted to only a single expression.
- Used only one-time.
- Lambda function is used when you need a function for a short period of time.
Basic syntax:
lambda arguments : expression
Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A lambda function that multiplies two numbers | |
multiply = lambda x, y: x * y | |
print(multiply(2, 2)) # output: 4 | |
# A lambda function that multiplies three numbers | |
multiply = lambda x, y, z: x * y * z | |
print(multiply(2, 2, 2)) # output: 8 | |
# A lambda function that adds 10 to the number passed in as an argument, and print the resul | |
a = lambda a: a + 10 | |
print(a(5)) # output: 15 |
Map: Map is a built-in function that takes a function object and iterable list (list/dictionary) as its parameter.
Basic syntax:
map (function_object, iterable_1, iterable_2, ... )
Few examples of Map in python:
Filter: It works similar way like map. The difference is it makes a new list from the element in the list that satisfy some condition. In case of of parameters, it takes a function and one iterable.
Basic syntax:
filter (function, iterable)
Normally used with lambda functions. Example is given below:
Basic syntax:
map (function_object, iterable_1, iterable_2, ... )
Few examples of Map in python:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_t(x): | |
return x + 1 | |
tuple_1 = (1, 2, 3, 4) | |
map_sample_1 = tuple(map(add_t, tuple_1)) | |
print(map_sample_1) # output: (2, 3, 4, 5) | |
# --------------------------------------------------------------- | |
def add_one(x): | |
return x + 1 | |
list_2 = [1, 2, 3, 4] | |
map_sample_2 = list(map(add_one, list_2)) | |
print(map_sample_2) # output: [2, 3, 4, 5] | |
# --------------------------------------------------------------------- | |
list_sample = [1, 2, 3, 4] | |
map_sample = list(map(lambda x: x + 1, list_sample)) | |
print(map_sample) # output: [2, 3, 4, 5] | |
# ---------------------------------- | |
def func(x): | |
return x + 1 | |
my_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 4} | |
my_dictionary = dict( | |
map(lambda kv: (kv[0], func(kv[1])), my_dictionary.items())) | |
print(my_dictionary) # output: {'a': 2, 'b': 3, 'c': 4, 'd': 5} |
Filter: It works similar way like map. The difference is it makes a new list from the element in the list that satisfy some condition. In case of of parameters, it takes a function and one iterable.
Basic syntax:
filter (function, iterable)
Normally used with lambda functions. Example is given below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seq = [1, 2, 3, 4] | |
# result contains odd numbers of the list | |
result = filter(lambda x: x + 1, seq) | |
print(list(result)) # Output: [1, 2, 3, 4] | |
# Does nothing on seq as the lambda function does not hold any condition | |
# filter only works on condition, so we need to give some conditional statement with lambda | |
result = filter(lambda x: x % 2 == 0, | |
seq) # returns the elements which are only divisible by two | |
print(list(result)) # output: [2, 4] |
Excellent Blog! I would Thanks for sharing this wonderful content.its very useful to us.I gained many unknown information, the way you have clearly explained is really fantastic.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course
Full Stack Training in Bangalore | Certification | Online Training Course
Full Stack Training in Hyderabad | Certification | Online Training Course
Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai
Full Stack Training
Full Stack Online Training
Nice article.keep up with your good work.
ReplyDeletePython classes in Pune