Lambda, Map, Filter, and Reduce in Python

Sadman Kabir Soumik
2 min readMay 6, 2020
Photo by Emma Matthews Digital Content Production on Unsplash

Lambda

A function doesn’t have any name. Is it possible? Yes, it is possible and these nameless functions are called Lambda functions. Lambda functions are known as the anonymous functions in Python. This kind of functions are declared without any name. It is used to create small functions.

Basic Syntax: lambda arguments : expression

These functions:

  • Can accept any number of argument.
  • Restricted to only a single expression.
  • Used only one-time.
  • Lambda function is used when you need a function for a short period of time.

Map

Map is a built-in function that takes a function object and iterable list (list/dictionary) as its parameter.

Basic Syntax: map (function_object, iterable_1, iterable_2, … )

Here, iterables can be list, tuple, dictionary etc that contains the data. We can also use lambda functions within map.

Filter

It works similar way like map. The difference is that it makes a new list from the element in the list that satisfy some condition. In case of of parameters, it takes a function and one iterable.

Basic Syntax: filter (function, iterable)

Reduce

It comes with the built-in module functools. Reduce applies with a function and a sequence, and always returns a single value. At first, the initial two elements of a list are picked, the applied condition works on that picked two elements and derives a result, then it goes to the next element and works on that element and previous derived result. The same process goes up to the last element of a sequence and outputs a single result.

Basic Syntax:

reduce(function, sequence)

Originally published at: https://sksoumik.blogspot.com/

Connect with me on LinkedIn

Email: sadmanks@gmail.com

--

--

Sadman Kabir Soumik

Artificial Intelligence | Cloud Computing | Back-End Engineering ☕️☕️