What is lambda and filter in python?

Summarizing other answers

Looking through the answers, we have seen a lot of back and forth, whether or not list comprehension or filter may be faster or if it is even important or pythonic to care about such an issue. In the end, the answer is as most times: it depends.

I just stumbled across this question while optimizing code where this exact question (albeit combined with an in expression, not ==) is very relevant - the filter + lambda expression is taking up a third of my computation time (of multiple minutes).

My case

In my case, the list comprehension is much faster (twice the speed). But I suspect that this varies strongly based on the filter expression as well as the Python interpreter used.

Test it for yourself

Here is a simple code snippet that should be easy to adapt. If you profile it (most IDEs can do that easily), you will be able to easily decide for your specific case which is the better option:

whitelist = set(range(0, 100000000, 27))

input_list = list(range(0, 100000000))

proximal_list = list(filter(
        lambda x: x in whitelist,
        input_list
    ))

proximal_list2 = [x for x in input_list if x in whitelist]

print(len(proximal_list))
print(len(proximal_list2))

If you do not have an IDE that lets you profile easily, try this instead (extracted from my codebase, so a bit more complicated). This code snippet will create a profile for you that you can easily visualize using e.g. snakeviz:

import cProfile
from time import time


class BlockProfile:
    def __init__(self, profile_path):
        self.profile_path = profile_path
        self.profiler = None
        self.start_time = None

    def __enter__(self):
        self.profiler = cProfile.Profile()
        self.start_time = time()
        self.profiler.enable()

    def __exit__(self, *args):
        self.profiler.disable()
        exec_time = int((time() - self.start_time) * 1000)
        self.profiler.dump_stats(self.profile_path)


whitelist = set(range(0, 100000000, 27))
input_list = list(range(0, 100000000))

with BlockProfile("/path/to/create/profile/in/profile.pstat"):
    proximal_list = list(filter(
            lambda x: x in whitelist,
            input_list
        ))

    proximal_list2 = [x for x in input_list if x in whitelist]

print(len(proximal_list))
print(len(proximal_list2))

A look at the syntax and usage of each function

What is lambda and filter in python?

Photo by Taras Shypka on Unsplash

Today’s piece covers using lambda, map, and filter functions in Python. We’ll be covering the basic syntax of each and walking through some examples to familiarize yourself with using them. Let’s get started!

Lambda

A lambdaoperatororlambdafunctionis used for creating small, one-time, anonymous function objects in Python.

Basic Syntax

lambda arguments : expression

Alambda operator can have any number of arguments but can have only one expression. It cannot contain any statements and returns a function object which can be assigned to any variable.

Example

Let’s look at a function in Python:

The above function’s name is add, it expects two arguments x and yand returns their sum.

Let’s see how we can convert the above function into a lambda function:

Inlambda x, y: x + y; x and y are arguments to the function and x + y is the expression that gets executed and its values are returned as output.

lambda x, y: x + y returns a function object which can be assigned to any variable, in this case, the function object is assigned to the add variable.

If we check the type of add, it is a function.

More importantly, lambda functions are passed as parameters to functions that expect function object as parameters such as map, reduce, and filter functions.

Map

Basic Syntax

map(function_object, iterable1, iterable2,...)

mapfunctions expect a function object and any number of iterables, such as list, dictionary, etc. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.

Example

In the above example, map executes the multiply2 function for each element in the list, [1, 2, 3, 4], and returns [2, 4, 6, 8].

Let’s see how we can write the above code using map and lambda.

Just one line of code!

Iterating Over a Dictionary Using Map and Lambda

In the above example, each dict of dict_awill be passed as a parameter to the lambdafunction. The result of the lambdafunction expression for each dict will be given as output.

Multiple Iterables to the Map Function

We can pass multiple sequences to the map functions as shown below:

Here, each i^th element of list_aand list_bwill be passed as an argument to the lambdafunction.

In Python3, the mapfunction returns an iteratorormap object which gets lazily evaluated, similar to how the zip function is evaluated. Lazy evaluation is explained in more detail in the zip function article.

We can’t access the elements of the map object with index nor we can use len() to find the length of the map object.

We can, however, force convert the map output, i.e. the map object, to list as shown below:

Filter

Basic Syntax

filter(function_object, iterable)

Thefilterfunction expects two arguments: function_object and an iterable. function_object returns a boolean value and is called for each element of the iterable. filter returns only those elements for which the function_object returns True.

Like the mapfunction, the filterfunction also returns a list of elements. Unlike map, the filterfunction can only have one iterable as input.

Example

Even number using filter function:

Filter list of dicts:

Similar to map, the filterfunction in Python3 returns a filter object or the iterator which gets lazily evaluated. We cannot access the elements of the filter object with index, nor can we use len() to find the length of the filter object.

If you enjoyed this article, kindly spread the word. To get updates for my new stories, follow me on medium and twitter

Other articles

  1. Zip in Python
  2. decorators in Python
  3. Concatenating two lists in Python
  4. List comprehensions in Python

What is lambda map and filter in Python?

The map() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item.

What is lambda used for in Python?

We use lambda functions when we require a nameless function for a short period of time. In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter() , map() etc.

What is filter () in Python?

Python's filter() is a built-in function that allows you to process an iterable and extract those items that satisfy a given condition. This process is commonly known as a filtering operation.

How do you filter a list in lambda?

To filter a list in Python, you can use the built-in filter() function..
The first argument is the filtering condition, defined as a function . ... .
The second argument is the iterable to be filtered—the lambda function checks for each element in the iterable whether the element pass the filter or not..