Python lambda print LEARONVITA

How To Use Python Lambda Functions | A Complete Beginners Guide [ OverView ]

Last updated on 02nd Nov 2022, Artciles, Blog

About author

Nirvi (Python developer )

Nirvi is a Python developer with 7+ years of experience in the Hadoop ecosystem, Sqoop, Hive, Spark, Scala, HBase, MapReduce, and NoSQL databases, such as HBase, Cassandra, and MongoDB. She spends most of her time researching technology and startups.

(5.0) | 19384 Ratings 2376
    • In this article you will learn:
    • 1.Introduction.
    • 2.Syntax of Lambda in Python.
    • 3.Why Is the Python Lambda Function Used?
    • 4.Which Functions in a Python Standard Library Leverage Lambdas?
    • 5.When to Use Lambda in Python?
    • 6.When to Avoid Lambda Functions in Python?
    • 7.Conclusion.

Introduction:

Lambda in Python is the keyword to explain anonymous functions. As might know, it is general to use the def keyword to explain a normal function. Similarly, lambda keyword is used when have to explain an anonymous function. Thus, an anonymous function is also known as lambda function in a Python. And can call these Python lambda functions as soon as define them.

Syntax of Lambda in Python:

Lambda arguments: expression Can use as many arguments as want in the lambda function, but it can have only a one expression. This expression is be evaluated and returned as a result. Let’s have a look at example to understand a concept of lambda function in a Python better.

Example: Using a Lambda Function in Python.

  • def squ(x)
  • return x*x
  • lambda_squ = lambda x: x*x
  • Printing both the outputs
  • print(squ(7))
  • print(lambda_squ(7))

Both the functions squ() and lambda_squ worked as an intended, and gave a same result. However, while explaining the normal function squ(), and had to describe a function and pass a return statement to get output. On other hand, while using a lambda function lambda_squ(),didn’t have to give a return statement. Thus lambda in a Python provides the more straightforward way to write a shorter and temporary functions.

Python Lambda Functions

Why Is Python Lambda Function Used?

  • When want to explain temporary nameless functions.
  • To pass it as argument to the other higher-order function that takes a functions as an argument.
  • To create the function that is callable as soon as explained .

Which Functions in Python Standard Library Leverage Lambdas?

Because of many benefits and uses of a Python lambda functions and can use them with the several built-in functions such as:

  • filter()
  • map()
  • reduce()

How to use a Lambda in Python with these built-in functions:

Example: Using a Python Lambda Functions With filter()As a name gives out, the filter() function is used to filter the sequence’s elements. And can pass any sequence like lists, sets, tuples, and so on. It takes into the account two parameters:

Function: The function needs to applied.

Sequence: The sequence on which a function needs to applied.

The Python filter() function applies a mentioned function to every element of the sequence and returns filtered result consisting of the elements that returned true after a function execution.In a below code, will use a two filters () functions. The first one to get only tnumbers divisible by a three, and second to get only numbers greater than 50 from list.

  • # Defining the list
  • # Passing a lambda function
  • divisible_lst = list(filter(lambda i:(i%3 == 0),example_lst))
  • print(divisible_lst)
  • # Passing a second lambda function
  • greater_lst = list(filter(lambda i:(i>50), example_lst))
  • print(greater_lst)
Lambda Functions in Python

Using a Lambda Function in Python With map():

The Python map() function is used to pass function through every item in an iterable. Like filter() function, even map() accepts the same two parameters: function and sequence. It iterates through the sequence and applies a specified function to each element.For this example, will use a same list as the last example, and two map() functions. The first one will apply the function to add a number to itself, and second one to multiply a number by itself.

  • # Defining the list
  • # Passing a lambda function
  • duble_lst = (list(map(lambda i:i*2, example_lst)))
  • print(duble_lst)
  • # Passing a second lambda function
  • square_lst = (list(map(lambda i:i*i, example_lst)))
  • print(square_lst)

Using a Lambda in Python With reduce():

The reduce() function in Python is used to apply the function to each element of the specified sequence. Python 3, reduce() became a part of a functools module. Hence, have to import a functools module to work with a reduce() function. It again accepts the two parameters: function and sequence.It will first apply a function to first two elements of the sequence. Next, it will take a result of the first execution as first argument and the next element of sequence as a second argument and apply function to them. The reduce() function continues doing this until no element is left in sequence.

When to Use a Lambda in Python?

At interpreter level, even a lambda functions are treated as a regular functions. Hence, it is need to understand which ones to use when. The best time to use a lambda functions in Python is:

  • When want to explain a short function for a temporary use.
  • While explaining a function that returns single expression.
  • When want to create a one-time use a function to pass as an argument to the another function (example: sort(), sorted(), min(), max().

When to Avoid a Lambda Functions in Python?

At core, the best use of a lambda in Python is to explain shorter one-time use functions. Hence, if a code is becoming too difficult, it is best to avoid it.Even though can write the complex code in one sentence with lambda functions, it is recommended to explain a regular function in such cases to make a code more straightforward and simple to understand.It is also recommended to avoid the lambda functions in Python if:

  • It does not follow a PEP 8 style guide.
  • It becomes the cumbersome and unreadable.
  • For raising an exception.

Conclusion:

Lambda functions are the small, anonymous functions explained using a lambda keyword.A lambda function can take to any number of arguments, but they contain a only one expression.Lambda function are automatically return a evaluated value of expression.Lambda functions are mostly used with the higher-order functions.Lambda functions help to make a code shorter but can lead to several problems in development like:Imprecise the tracebacks.Inability to include the statements like raise in a function body.No type to annotation support.A Poor readability.

Are you looking training with Right Jobs?

Contact Us

Popular Courses