At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

How To Use Python Lambda Functions With Examples

  • By Rajasekar
  • November 13, 2019
  • 929 Views

Python Lambda is an anonymous function. It is a function that can take any number of arguments but can have only one expression. It is nameless and ‘def’ (keyword) is used for normal Python functions. Lambda is considered as a keyword in Python that represents anonymous function inside another function.
We all know, everything in Python is an object. Everything here explained is pretty easy to understand. Take a look at the basic function below.

>>> lam_fun = lambda s: s.casefold()
>>> lam_fun
Output : <function <lambda> at 0x7f264d5b31e0>
>>> def lam_fun(s): return s.casefold()
>>> lam_fun
Output : <function lam_fun at 0x7f247e68fea0>

Lambda rule in Python

Lambda functions accept only one expression. If your function has multiple expressions or statements, you can define a function apart from Lambda.
A Lambda function is created using the lambda keyword and the syntax as follows,

Syntax

lambda argument_list: expression

>>> f = lambda x, y : x + y
>>> f(1,1)
Output : 2

What are the uses of Lambda Functions?

The key advantage of the lambda operator is that it can be seen. Like when it is used in combination with the built-in functions like map, filter.

Syntax for Map

map(func, seq)

map() is a function with two arguments, Map is a Python built-in function that takes in a function and a sequence as arguments and then calls the input function on each item of the sequence.

#Example

Here I solve the problem which is a combination of int and str list. Let’s assume that it is rendered from the bike number plate in the list format. Here we are using the map function to get a proper result “Bike no.”

>>> no = [“T”, “N”, 3, 4, “A”, 6, 4, 5, 6]
>>> no = “”'.join(map(str, no))
>>> no
Output : ‘TN34A6456’

Syntax for filter

filter(func, seq)

The function filter(function, list) is the way to filter out all the elements of a list, for which the function returns True. This filter is another built-in function that actually filters an iterable object or any sequence.

#Example

In this example, I solve a basic and simple task, a list of numbers are given and we will be finding out the old number’s in the given list.

>>> no = [1, 2, 3, 4, 5, 6]
>>> R = list(filter(lambda x: x % 2, no))
>>> R
Output : [1, 3, 5]

Syntax to reduce

reduce(func, seq)

The function reduce(func, seq) applies the function func() continually to the sequence seq. It returns a single value. In Python 2, reduce() was a built-in function. However, in Python 3, it is moved to functools module.

How Reduce Function works?

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        try:
            initializer = next(it)
        except StopIteration:
            raise TypeError('reduce() of empty sequence with no initial value')
    accum_value = initializer
    for x in it:
        accum_value = function(accum_value, x)
    return accum_value

#Example

A list of numbers is given and we have to get a total of it. Before we import the reduce function from functools module. The sum() built-in function can do this task very simple but in this example, we will do it in reduce. For more details, you can refer to the above Reduce function.

>>> from functools import reduce # python 3
>>> no = [1, 2, 3, 4, 5, 6]
>>> R = reduce(lambda x,y: x+y, no)
>>> R
Output : 21

Last thoughts on Python Lambda Function

Lambda functions are single-expression functions and nameless hard to identify the object. Python Lambda can’t use regular statements. instead of that includes a return statement (Boolean or value).
Now, I hope that everyone could understand my points on Python Lambda. If you are working as an individual or a team see that you gain some knowledge before using the function. For beginners, it might be complex sometimes. If you are a Python developer you might feel comfortable with code readability.
Want to develop an outstanding application with Python? Take a look at our expertise and Hire a developer to make your projects a success.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions

Rajasekar

A Senior Software developer. Specialist in Python, Django, Flask, PostgreSQL, and AWS. Currently working on Machine Learning, Raspberry Pi and NLP technologies. This ecstatic techie is always full of ideas that make life easier with machines. He is also a biker by passion.