Course Content

Anonymous/Lambda Functions In Python

As we have studied function in the previous lecture, we know that if a program has a large piece of code that is required to be executed repeatedly, then it will be better to implement that piece of code as a function. Functions improve code reusability, modularity, and integrity.

In this tutorial, we will see how to declare Anonymous functions and how to return values from them.

As we have studied in the previous lecture, the syntax for function declaration is as follows:

def function_name ():

What is Anonymous function?

In Python programming, an anonymous function or lambda expression is a function definition that is not bound to an identifier (def).

The anonymous function is an inline function. The anonymous functions are created using a lambda operator and cannot contain multiple expressions.

 The following example will show how anonymous functions work: 

result = lambda n1, n2, n3: n1 + n2 + n3;
print ("Sum of three values : ", result( 10, 20, 25 ))

In the above code, we have created an anonymous function that adds three numbers. The function is stored in a variable named result. The function can then be called using this variable. In the above code, the function has been called with three different parameter values for the function call.

Anonymous functions can accept inputs and return the outputs, just like other functions do.

Why use Python Lambda Functions?

The main objective of anonymous functions is that, when we need a function just once, anonymous functions come in handy. Lambda operator is not only used to create anonymous functions, but there are many other uses of the lambda expression. We can create anonymous functions wherever they are needed. Due to this reason, Python Lambda Functions are also called as throw-away functions which are used along with other predefined functions such as reduce(), filter(), and map(). 

These functions help reduce the number of lines of the code when compared to named Python functions.

Significant Differences Between Lambda Expressions And Simple Functions.

  1. Can be passed immediately with or without variables.
  2. They are inline functions.
  3. Automatic return of results.
  4. There is neither a document string nor a name.

Python List sort():

Sorting means arranging data systematically. If the data we want to work with is not sorted, we will face problems finding the desired element.

The Python language, like other programming languages, can sort data.

Python has an in-built method i.e. sort(), which is used to sort the elements of the given list in a specified ascending or descending order. There is also a built-in function i.e. sorted(), that builds a new sorted list from an iterable (list, dictionary, etc.).

The syntax of the sort() method is:

list.sort(key=myFunc ,reverse=True|False)

Parameter Values:-

Parameters

Values

key

In the key parameter, we specify a function that is called on each list element before making comparisons. 

reverse

This is optional. False will sort the list in ascending order, and true will sort the list in descending order.

Default is reverse=False.

 

Sort() does not return any value, but it changes from the original list.

 

Code file as described in the video

# Lambda functions or anonymous functions
# def add(a, b):
#     return a+b
#
# # minus = lambda x, y: x-y
#
# def minus(x, y):
#     return x-y
#
# print(minus(9, 4))


a =[[1, 14], [5, 6], [8,23]]
a.sort(key=lambda x:x[1])
print(a)
  

Comments(2)

sahib007 10 months, 3 weeks ago
def a_first(a):
    print(a[1])
a = [[1, 14], [5, 6], [8, 23]]
a.sort(key=a_first)
print(a)
# Harry bhai ye code to run hi ni ho rha h
nazishali 3 months, 3 weeks ago
print ki jaga return karo tab run ho ga

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here