We have the answers to your questions! - Don't miss our next open house about the data universe!

Python Lambda functions: principles and benefits

- Reading Time: 3 minutes
Python et fonctions Lambda : principe et intérêts

For small operations, regular functions in Python can take up a lot of space, making syntax difficult to read. This is why Lambda functions are so useful in Python.

How do you use them and why? Find out in this article.

 

💡Related articles:

Matplotlib: Master Data Visualization in Python
Python Crash Course: Get started
Mastering Machine Learning in Python: Data-Driven Success
Python Programming for Beginners – Episode 3
Django: All about the Python web development framework
NumPy : the most used Python library in Data Science

Lambda functions in Python, anonymous functions

In Python, a Lambda function is an anonymous function. In other words, a function declared without a name. Its syntax is as follows:

Lambda arguments: expression

As this is a single-line syntax, the function is much more readable. There’s no need to search for the argument on another line. Although the syntax is different from regular functions declared with the def keyword, they behave in the same way. It’s just more concise.

Aside from its lack of a name and its conciseness, the Lambda functions in python are characterized by the fact that they contain only one expression. On the other hand, they can have several arguments.

4 exemples de fonctions Lambda

To help you better understand the difference between Python’s Lambda functions and regular functions, here are a few examples:

Lambda and sum functions

If you want to calculate the sum of two values, your code can be one of these two formulas.

Regular Function in Python Lambda Function in Python
1 def sum_classic( a , b ):
2 return a + b
sum_Lambda = Lambda a,b : a+b

Lambda function and map

With the map function, you can double each element of a list.

Here are the differences between the Lambda function and the regular function.

Regular Function in Python Lambda Function in Python
1 def doubler(x):
2 return x * 2
3 list = [1, 2, 3, 4, 5]
4 result_def = list(map(doubler, list))
5 print(result_def)
1 list = [1, 2, 3, 4, 5]
2 result_lambda = list(map(lambda x: x * 2, list))
3 print(result_lambda)

This will display [2, 4, 6, 8, 10].

Lambda and filter functions

With the map function, you can filter certain elements. In this example, the aim is to filter out even numbers from a list.

Here are the differences between the Lambda function and the regular function.

Regular Function in Python Lambda Function in Python
1 def is_even(x):
2 return x % 2 == 0
3 list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 result_def = list(filter(is_even, list))
5 print(result_def)
1 list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 result_lambda = list(filter(lambda x: x % 2 == 0, list))
3 print(result_lambda)

In both cases, it will display [2, 4, 6, 8].

Lambda and sorted functions

With the map function, you can sort a list. In this example, a list of tuples will be sorted according to the second element.

Here’s the code to type for both types of function:

Regular Function in Python Lambda Function in Python
1 def key_sort(tuple):
2 return tuple[1]
3 tuples = [(1, 5), (3, 2), (8, 10), (4, 7)]
4 result_def = sorted(tuples, key=key_sort)
5 print(result_def)
1 tuples = [(1, 5), (3, 2), (8, 10), (4, 7)]
2 result_lambda = sorted(tuples, key=lambda x: x[1])
3 print(result_lambda)

This will display [(3, 2), (1, 5), (4, 7), (8, 10)].

Apart from these examples, Lambda functions on Python are best used with simple functions for a one-off operation, such as apply(), sorted(), applymap() or reduce().

Lambda function use cases in Python

The Lambda function in Python can be used in the following situations:

  • Small operations: the return value is calculated by evaluating an expression on a single line of code. In this case, defining a complete function with def may seem too cumbersome.
  • One-off operations: this means you don’t need to name the function, since it’s only used once. If you need to repeat it or refer to it elsewhere in the same module, it’s best to use normal functions.
  • Built-in functions: such as the map(), filter(), apply(), sorted(), sum() and reduce() functions mentioned above.

In practical terms, Lambda functions are useful for sorting Python data structures, such as lists and dictionaries. That said, the choice between Lambda and regular functions is mainly a question of style and readability, because in the end, the result is the same.

Good to know: Lambda functions exist in many programming languages (Java, C#, C++). But with Python, they don’t add any extra functionality. This is not necessarily the case with other computer languages.

Use Lambda functions with DataScientest

Whether you’re using normal or Lambda functions with Python, you need to be well trained to master this programming language. With DataScientest, you can do just that. In our program, you’ll learn how to code all kinds of operations, from the simplest to the most complex.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox