# Python Function-lambda |filter, map, reduce.

- Link: https://technotaught.com/python-function-lambda-filter-map-reduce/
- Published: 2019-06-27T14:02:17+05:30
- Author: Ashishkumar Vishwakarma

## Python Function

Pyhon funcrtion has 4 types that is Lambda, filter, map, reduce function.

## Lambda

 * A lambda function is a small anonymous function.

 * A lambda function can take any number of arguments, but can only have one expression.

#### Syntax

**Lambda arguments: expression**

The expression is executed and the result is returned:

 [Let’s look](https://www.facebook.com/technotaughts/?modal=admin_todo_tour) at 
this example and try to understand.

    ```javascript
    #Square of given no
    f=lambda x : x*x
    print(f(7))

    #Largest of two no
    f=lambda x,y: x if (x>y) else y
    print("Largest no",f(5,7))

    #Calculate the sum of two no
    f=lambda a,b: a+b
    print("add:",f(2,3))

    #calculate the multiplication
    f=lambda a,b: a*b
    a=int(input("Enter value:"))
    b=int(input("Enter value:"))
    print("Mul:",f(a,b)
    ```

Output.

    ```wp-block-code
    >>> 
    ========== RESTART: C:/Users/User/Desktop/python/practise/lambda.py ==========
    49
    Largest no 7
    add: 5
    Enter value:2
    Enter value:3
    Mul: 6
    >>> 
    ```

### Lambda using Filter

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_. filter function also returns a list of element .

## Filter

 * Filter=Take only true value and Store.
 *  The filter () function in Python takes in a function and a list as arguments.
   This offers an elegant way to filter out all the elements of a sequence “sequence”,
   for which the function returns True.

    ```language-markup
    li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
    odd_list = list(filter(lambda x: (x%2 != 0) , li))
    print(odd_list) 
    even_list = list(filter(lambda x: (x%2 == 0) , li))
    print(even_list)
    ```

> Output:
>  [5, 7, 97, 77, 23, 73, 61] [22, 54, 62]

## Map Function

 * Map maps element of the list to another value as per expression in the lambda

    ```language-markup
    square_list = list(map(lambda x: x*x,li))
     print(square_list) 
    cube_list = list(map(lambda x:x*x*x,li)) 
    print(cube_list) 
    ```

 

> Output:
>  [25, 49, 484, 9409, 2916, 3844, 5929, 529, 5329, 3721] [125, 343, 10648,
> 912673, 157464, 238328, 456533, 12167, 389017, 226981]

## Reduce Function

 * Reduces a list to result

    ```language-markup
    li = [5, 8, 10, 20, 50, 100] 
    sum = reduce((lambda x, y: x + y), li)
    print(sum) mult = reduce((lambda x,y:x*y),li) 
    print(mult)
    ```

> “output 193 40000000
> Output :
> 19340000000

[Dsa practical](https://technotaught.com/category/dsa-practical/)

### Share this:

 * [  X ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=twitter)
 * [  Facebook ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=facebook)
 * [More](https://technotaught.com/python-function-lambda-filter-map-reduce/#)

 * [  WhatsApp ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=linkedin)
 * [  Email ](https://technotaught.com/python-function-lambda-filter-map-reduce/?subject=%5BShared%20Post%5D%20Python%20Function-lambda%20%7Cfilter%2C%20map%2C%20reduce.&body=https%3A%2F%2Ftechnotaught.com%2Fpython-function-lambda-filter-map-reduce%2F&share=email)
 * [  Reddit ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=reddit)
 * [  Tumblr ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=pinterest)
 * [  Telegram ](https://technotaught.com/python-function-lambda-filter-map-reduce/?share=telegram)
 * [  Print ](https://technotaught.com/python-function-lambda-filter-map-reduce/#print?share=print)

### _Related_

## Comments

### By: Anessa B

- Link: https://technotaught.com/python-function-lambda-filter-map-reduce/#comment-138
- Published: 2020-04-24T09:34:28+05:30

Hello there, You have done a fantastic job. I’ll certainly digg it and for my part
recommend to my friends. I’m sure they’ll be benefited from this site.\|
