In Python, the “lambda” keyword allows you to define a lambda function. This is a function that returns a single expression in one line. It’s like a shortcut for using ‘def’ to create a normal function, which requires at least two lines.

Syntax: lambda arguments : expression

Why They’re Called Lambda Functions?

Lambda, or λ, is the 11th letter of the Greek alphabet. Due to the use of the Greek alphabet in mathematics, Alonzo Church ended up using it in the 1930’s when describing a concept he called Lambda Calculus. This is a formal system describing any possible computation — something like a purely mathematical programming language.

Lambda calculus is so-called because it uses Lambda (λ) to represent functions, which also never have names. The Lips programming language copied this concept, and Python copied it from Lisp.

Example Of Using Lambda Function In Python:

Program for the list of puppies with their cuteness ratings:

class Puppy:
def __init__(self, name, cuteness):
self.name = name
self.cuteness = cuteness

def __repr__(self):
return f"Puppy({self.name!r}, {self.cuteness!r})"


puppies = [Puppy("Doggo", 100), Puppy("Kevin", 200), Puppy("Bock", 50)]

Use With list.sort() and sort()

The list.sort() method takes with an optional key argument. This is a function to map the list items to values to sort them by. We can use it to sort our puppies by their increasing cuteness ratings, by passing key as a function that extracts a given puppy’s cuteness value.

The sorted() built-in similarly takes a key argument, but it takes with any iterable instead of just lists, so you’ll often see lambda used in conjunction with it.

list.sort()

def get_cuteness(puppy): return puppy.cuteness puppies.sort(key=get_cuteness)

>>> puppies [Puppy('Bock', 50), Puppy('Doggo', 100), Puppy('Kevin', 200)]

sort()

puppies.sort(key=lambda puppy: puppy.cuteness)

>>> puppies [Puppy('Bock', 50), Puppy('Doggo', 100), Puppy('Kevin', 200)]

sorted()

>>> sorted(puppies, key=lambda p: p.cuteness)
[Puppy('Bock', 50), Puppy('Doggo', 100), Puppy('Kevin', 200)]

Use With filter()

The filter() built-in takes a function and an iterable, and returns the items from the iterable for which the function returned true. We can use it to filter our puppies to only the cutest ones, by passing key as a function that returns if a given puppy has enough cuteness.

>>> list(filter(lambda p: p.cuteness >= 100, puppies))
[Puppy('Doggo', 100), Puppy('Kevin', 200)]

Use With map()

The map() built-in takes a function and an iterable, and returns a new iterable of the results of applying the function on the items in the passed iterable. We can use it to extract our puppies’ names into a list of strings.

>>> list(map(lambda p: p.name, puppies))
['Doggo', 'Kevin', 'Bock']

Use With reduce()

The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and an iterable and a new reduced result is returned. This performs a repetitive operation over the pairs of the iterable. The reduce() function belongs to the functools module.

There are 5 Advantages of Normal Function.

Normal functions have a number of advantages that the lambda syntax does not allow.

  1. Naming

The operator Module

A third alternative to writing lambda functions is to use the standard library’s operator module. This module contains some predefined functions and function factories, which can replace the most common use cases for lambda functions. Let’s look at both of these separately, factories first.

import operator get_cuteness = operator.attrgetter('cuteness')

>>> get_cuteness(puppies[0]) 100

Operator Fucntions

Another set of functions offered by the operator module are its wrappers of Python’s operators (hence the module’s name). These can replace another bunch of common use cases for lambda functions.

from functools import reduce cutenesses = [p.cuteness for p in puppies]

>>> reduce(lambda a, b: a * b, cutenesses) 1000000

Graduated in BSC(Comp Sci). Pursuing MSC. Specialization: html,java,c,c++,python