Understanding Python Lambda Expressions: Syntax, Examples, and Use Cases
Python's lambda expressions provide a concise way to create anonymous functions, and this guide explains their syntax, demonstrates simple to complex examples—including arithmetic, conditional logic, and sorting—and compares them with traditional functions and built‑in utilities like filter and list comprehensions.
When we need to do something simple and prefer quick work rather than formally naming a function, lambda expressions are ideal. Lambda expressions are also called anonymous functions.
In Python, lambda expressions are a short way to declare small anonymous functions (no need to give them a name).
Lambda functions behave like regular functions declared with def . They are useful for defining small functions concisely. They can only contain a single expression, so they are not suitable for functions with control‑flow statements.
Lambda function syntax
lambda arguments: expression
Lambda functions can have any number of arguments but only one expression.
Code examples
# Lambda function to compute the square of a number square = lambda x: x ** 2 print(square(3)) # Output: 9
# Traditional function to compute the square of a number def square1(num): return num ** 2 print(square1(5)) # Output: 25
In the lambda example, lambda x: x ** 2 creates an anonymous function object that is assigned to the name square , allowing it to be called like a regular function.
Lambda function examples
Basic
lambda_func = lambda x: x**2 # returns the square of an integer print(lambda_func(4)) # 16
Intermediate
lambda_func = lambda x: True if x**2 >= 10 else False print(lambda_func(3)) # False print(lambda_func(8)) # True
Complex
linuxmi_dict = {"X": 1, "Y": 2, "Z": 3} sorted_keys = sorted(linuxmi_dict, key=lambda i: linuxmi_dict[i] % 3) print(sorted_keys) # ['Z', 'X', 'Y']
Use case: filtering odd numbers from a list
Using a for loop:
linuxmi_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] filtered = [] for num in linuxmi_list: if num % 2 != 0: filtered.append(num) print(filtered) # [1, 3, 5, 7, 9, 11]
Using a list comprehension:
filtered = [x for x in linuxmi_list if x % 2 != 0]
Using the built‑in filter function with a lambda:
filtered = filter(lambda i: i % 2 != 0, linuxmi_list) print(list(filtered)) # [1, 3, 5, 7, 9, 11]
Note: In Python 3 the filter function returns a generator, so you must wrap it with list() to see the results; in Python 2 it returns a list directly.
The filter call applies the lambda to each element of linuxmi_list ; elements for which the lambda returns False are discarded, leaving only the odd numbers.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.