Python- lambda, map and filter



lambda operator or lambda function is used for creating small, one-time and anonymous function objects in Python.

Basic syntax:
lambda arguments: expression

lambda operator can have any number of arguments, but it can have only one expression. It cannot contain any statements and it returns a function object which can be assigned to any variable.

Function in python:
def add(x, y):
return x + y

Call the function

add(2, 3) # Output: 5

Above function name is add, it expects two arguments x and y and returns their sum.

Let’s see how we can convert the above function into a lambda function:
add = lambda x, y : x + y

print add(2, 3) # Output: 5
In lambda x, y: x + y; x and y are arguments to the function and x + y is the expression which gets executed and its values is returned as output.

lambda x, y: x + y returns a function object which can be assigned to any variable, in this case, the function object is assigned to the added variable.

type (add) # Output: function
If we check the type of add, it is a function.

Mostly lambda functions are passed as parameters to a function which expects a function object as a parameter like a map, reduce, filter functions


Basic syntax
map(function_object, iterable1, iterable2,…)

map functions expect a function object and any number of iterables like a list, dictionary, etc. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.

def multiply2(x):
return x * 2

map(multiply2, [1, 2, 3, 4]) # Output [2, 4, 6, 8]
In the above example, map executes multiply2 function for each element in the list i.e. 1, 2, 3, 4 and returns [2, 4, 6, 8]

Let’s see how we can write the above code using map and lambda.
map(lambda x : x*2, [1, 2, 3, 4]) #Output [2, 4, 6, 8]

Just one line of code and that’s it.


Basic syntax:
filter(function_object, iterable)

filter function expects two arguments, function_object and an iterable. function_object returns a boolean value. function_object is called for each element of the iterable and filter returns only those element for which the function_object returns true.

Like map function, filter function also returns a list of element. Unlike map function filter function can only have one iterable as input.


Even number using filter function
a = [1, 2, 3, 4, 5, 6]
filter(lambda x : x % 2 == 0, a) # Output: [2, 4, 6]

Filter list of dicts
dict_a = [{‘name’: ‘python’, ‘points’: 10}, {‘name’: ‘java’, ‘points’: 8}]

filter(lambda x : x[‘name’] == ‘python’, dict_a) # Output: [{‘name’: ‘python’, ‘points’: 10}]
Similar to map, filter function in Python3 returns a filter object or the iterator which gets lazily evaluated. Neither we can access the elements of the filter object with index nor we can use len() to find the length of the filter object.

list_a = [1, 2, 3, 4, 5]

filter_obj = filter(lambda x: x % 2 == 0, list_a) # filter object <filter at 0x4e45890>

even_num = list(filter_obj) # Converts the filer obj to a list

print(even_num) # Output: [2, 4]

source link: https://medium.com/@happymishra66/lambda-map-and-filter-in-python-4935f248593