Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Lambda functions, also known as anonymous functions, are small, single-expression functions that are defined using the lambda
keyword in Python. Lambda functions are used for short, simple operations that can be written in a single line, and they can be used wherever function objects are required.
In this tutorial, we will explore the syntax and usage of lambda functions in Python.
The syntax for creating a lambda function is as follows:
lambda arguments: expression
Lambda functions can take any number of arguments but must have only one expression. The expression is evaluated and returned when the lambda function is called.
Let's look at an example of a lambda function that adds two numbers:
add = lambda x, y: x + y result = add(5, 3) print(result) # Output: 8
In this example, the lambda function takes two arguments, x
and y
, and returns their sum. The lambda function is assigned to the variable add
, which can be used like any other function.
Lambda functions are often used as arguments for higher-order functions like map()
, filter()
, and sorted()
that take a function as input and apply it to a sequence of elements.
Example using map()
:
numbers = [1, 2, 3, 4] squares = list(map(lambda x: x**2, numbers)) print(squares) # Output: [1, 4, 9, 16]
In this example, the lambda function squares each element in the numbers
list, and the map()
function applies the lambda function to each element. The result is a list of squared numbers.
Example using filter()
:
numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) # Output: [2, 4, 6]
In this example, the lambda function returns True
for even numbers and False
for odd numbers. The filter()
function applies the lambda function to each element in the numbers
list and retains the elements for which the lambda function returns True
.
Lambda functions are limited compared to regular functions defined with the def
keyword. Regular functions can have multiple expressions, statements, and a name, while lambda functions can have only one expression and no name.
Example of a regular function equivalent to the add
lambda function:
def add(x, y): return x + y result = add(5, 3) print(result) # Output: 8
In conclusion, lambda functions in Python are anonymous, single-expression functions defined using the lambda
keyword. They are used for short, simple operations that can be written in a single line, and they can be used wherever function objects are required. Lambda functions are particularly useful when working with higher-order functions like map()
, filter()
, and sorted()
.
Creating anonymous functions with lambda
in Python:
lambda
allows you to create small, anonymous functions without a formal definition. They are often used for short, simple operations.add = lambda x, y: x + y result = add(5, 3) print(result) # Output: 8
Lambda functions vs named functions in Python:
# Lambda function add = lambda x, y: x + y result_lambda = add(5, 3) # Named function def add_named(x, y): return x + y result_named = add_named(5, 3)
Lambda functions for simple one-liners in Python:
numbers = [1, 2, 3, 4, 5] square = list(map(lambda x: x**2, numbers)) print(square) # Output: [1, 4, 9, 16, 25]
Passing lambda functions as arguments in Python:
def operate_on_numbers(operation, x, y): return operation(x, y) result = operate_on_numbers(lambda a, b: a * b, 5, 3) print(result) # Output: 15
Using lambda functions with filter()
and map()
in Python:
filter()
and map()
for concise data manipulation.numbers = [1, 2, 3, 4, 5] # Using filter with lambda evens = list(filter(lambda x: x % 2 == 0, numbers)) # Using map with lambda squares = list(map(lambda x: x**2, numbers))
Lambda functions in list comprehensions in Python:
numbers = [1, 2, 3, 4, 5] # Using lambda in list comprehension squared_even_numbers = [x**2 for x in numbers if x % 2 == 0]
Common use cases and scenarios for lambda functions:
# Sorting a list of tuples based on the second element pairs = [(1, 5), (2, 3), (3, 8)] sorted_pairs = sorted(pairs, key=lambda x: x[1])