1. Home
  2. Docs
  3. Python 201
  4. Lambda Functions

Lambda Functions

 
In this lesson you learn about lambda functions in Python,

They are simple, and yet confusing at times.

Let’s try understand them, by exploring some simple examples.
 

Anonymous Functions or lambda functions

 

Anonymous function is a function without a name.

Like in other programming languages, in Python we can create anonymous functions.

Anonymous functions are created using a lambda keyword.

Generally these functions just look like an expression, that’s why there

are also known as lambda expressions
 
syntax:
  

lambda [argument list] : code statement

 
lambda is keyword, argument list is optional, and a ( : ) colon, fallowed by
 
code statement.

Let’s look at some examples
 

Creating a lambda function

 
Ex 1:
 
simplest lambda function.
 

lambda : 10

output:

<function __main__.>lambda

  
Creates a anonymous function which returns 10.

Note there are no ( ) brackets and return keyword.

lambda functions evaluates the expression

and automatically returns the result.

You can assign lambda function to a variable and call it with ( ).
  

lambda : 10

value = lambda : 10

value()

output:

10

  

lambda function with one argument

 
Ex 2:
 

Multiple x with 10

ten_times = lambda x : x * 10

ten_times(2)

  
output:

20

  
Here lambda function takes x as argument and multiples it by 10

and return result as 20.

 

lambda function with two arguments

 
Ex 3:
 

Multiply x by y

multiply = lambda  x , y : x * y


multiply(3,4)

output:

12

  
this lambda function takes two arguments, x and y, separated by ( , ) comma.

Evaluates the express x * y and returns the product.
 

lambda function with conditional evaluation

 

Ex 4:
 
get the biggest of x and y
 

bigger = lambda  x , y : x if x > y else y

print(bigger(4,5))

print(bigger(4,3))

  
here lambda takes, x and y, evaluates x if x > y else y

it’s ternary expression, its same as
 

if x > y:  

    return x

else:

    return y

  

Using built methods of string

 
Ex 4:
 
Return uppercase string.
 

to_upper = lambda s : s.upper()

to_upper('just learn python')

output:

'JUST LEARN PYTHON'

  
Ex 5:
 
Compare two string for their length.
  

is_bigger = lambda s1,s2 : len(s1) > len(s2)

is_bigger('just learn python','learn python')

output:

True

 

Using lambda function as a helper function

 
Ex 6:
 
Sort list by last letter.
  

['James','Fanny','Steve','Kieth','Angie']


sort_helper = lambda s : s[-1]
    
names = ['James','Fanny','Steve','Kieth','Angie']

sorted(names, key=sort_helper)

output:

['Steve', 'Angie', 'Kieth', 'James', 'Fanny']

 

Using lambda function in list comprehension

  

sort_helper = lambda s : s[-3]
    
names = ['James','Fanny','Steve','Kieth','Angie']

[sort_helper(name) for name in names]

output:

['m', 'n', 'e', 'e', 'g']

 
lambda functions are meant to be small,
 
You cann’t have if-elif-else, while, try, etc inside a lambda expression.

lambda function often used with map, filter,reduce or

as helper functions to builtin functions.