Skip to content

How to use the Python Lambdas?

Python lambdas are so-called anonymous functions with which you can quickly define functions that can have multiple inputs, but only one output. Such anonymous functions are not only used in Python, but also in other programming languages like Java, C#, or C++.

How are normal functions defined in Python?

In Python, the definition of a function is marked with “def”. Then its name is defined and the arguments with names are listed in round brackets. The following function named “sum” simply takes two arguments and adds them together:

Python Lambda

The advantage of these functions is that they can be called via a unique name. This helps both to better describe the functionality and to reference the function again at a later time. In addition, the arguments can also have names that help to better understand the result. Another advantage is that explicitly defined functions can also output more than one argument.

Our initial function, for example, can output the difference between the two arguments in addition to the sum:

Python Lambda

What are anonymous functions?

However, there is also a second way how functions can be defined in Python. With the help of so-called anonymous functions, these can be defined with just a few lines, usually even just one. These anonymous functions also exist in other programming languages, such as Java or C#. In Python, the argument “lambda” is used for this, which is why anonymous functions in Python are often simply called Python lambdas.

These functions can be easily defined with the parameter “lambda”, the naming of the variables, and the expression that the function should calculate:

Python Lambda

This example also clearly shows why Python lambdas are anonymous functions: The function itself cannot be given a name, it can only be stored in a variable (“function”).

So if we want to recreate the sum function from our initial example using Python Lambdas, here’s what it looks like:

Python Lambda

As you can see, more than one argument can be passed to Python Lambdas. However, we cannot recreate the function “sum_difference” with Python lambdas, because the anonymous functions can only output one result. So instead, two different functions must be defined and also called twice:

Python Lambda

Why should you use Python Lambdas?

There are several cases where it can be useful to use Python lambdas:

  • When simple functions are defined that have only one output and a small number of inputs. In large projects it can make sense not to define such a function explicitly, in order to save space and not to cause unnecessary confusion. Furthermore one saves problems with the inheritance, etc. in extensive classes.
  • The same argument is also valid if functions are used only once. Then one can do without the explicit definition since anyway nobody must access the function again.
  • In addition, anonymous functions can ensure that the content of the function is faster and easier to understand since it is defined in a single line. Explicitly defining functions can result in losing some comprehensibility.
  • There are functions in Python, such as “filter” or “map”, that take functions as input. So in these cases, it makes sense to use Python Lambda.

How does the filter() function work with Python lambdas?

The function “filter()” can be used to filter a list with a certain condition. The result is a new list that consists only of those elements that meet the condition. Without the explicit function you could also solve this as follows:

Python Lambda

The function “filter()” has a total of two inputs, firstly the list to be filtered and secondly the condition to be used for filtering. This filter condition must be a function, which is why Python lambdas are suitable for this:

Python Lambda

For such applications, Python Lambdas are of course also an optimal alternative.

This is what you should take with you

  • Anonymous functions are used to quickly define functions that do not have to have a name.
  • This can be useful if functions are to be used only once or if you want to keep the namespace of variables and functions small.
  • In Python, anonymous functions are used with the parameter “lambda”, which is why anonymous functions in Python are also called Python lambdas.

Thanks to Deepnote for sponsoring this article! Deepnote offers me the possibility to embed Python code easily and quickly on this website and also to host the related notebooks in the cloud.

Other Articles on the Topic of Python Lambdas

  • w3schools offers even more concrete examples of the use of Python Lambdas.
close
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Cookie Consent with Real Cookie Banner