codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

26 / 68 topics
24Python Functions25Function Arguments (*args, **kwargs)26Python Lambda Functions27Python Namespace & Scope (Global/Local)28Python Closures29Python Recursion
Tutorials/Python Programming/Python Lambda Functions
🐍Python Programming

Python Lambda Functions

Updated 2026-05-15
30 min read

Python Lambda Functions

Lambda functions are a powerful feature in Python that allow you to create anonymous functions. These functions are defined without a name and can be used wherever function objects are required. They are particularly useful for short, throwaway functions that are not complex enough to warrant a full def statement.

Understanding lambda functions is essential for writing concise and efficient code, especially when working with higher-order functions like map(), filter(), and sorted(). In this tutorial, we'll explore the syntax of lambda functions, their use cases, and how they compare to regular functions defined with def.

Introduction

Lambda functions are small anonymous functions defined using the lambda keyword. They can have any number of arguments but only one expression. The expression is evaluated and returned when the function is called.

Lambda functions are often used in situations where a simple function is needed temporarily, such as in functional programming techniques or when working with built-in functions that expect a function object as an argument.

Core Content

Syntax of Lambda Functions

The basic syntax of a lambda function is:

Python
1lambda arguments: expression

Here's a simple example to illustrate the syntax:

lambda_syntax.py
1# Define a lambda function that adds two numbers
2add = lambda x, y: x + y
3
4# Call the lambda function
5result = add(3, 4)
6print(result)
Output
7

In this example, lambda x, y: x + y defines an anonymous function that takes two arguments, x and y, and returns their sum. The lambda function is assigned to the variable add, which can then be called like a regular function.

Using Lambda with map()

The map() function applies a given function to each item of an iterable (like a list) and returns a map object (which can be converted to a list). Lambda functions are often used with map() for concise transformations.

Here's an example that demonstrates using lambda with map():

lambda_map.py
1# List of numbers
2numbers = [1, 2, 3, 4, 5]
3
4# Use map() to square each number
5squared_numbers = list(map(lambda x: x ** 2, numbers))
6
7print(squared_numbers)
Output
[1, 4, 9, 16, 25]

In this example, map() applies the lambda function lambda x: x ** 2 to each element in the list numbers, resulting in a new list of squared numbers.

Using Lambda with filter()

The filter() function constructs an iterator from elements of an iterable for which a function returns true. Like map(), lambda functions are commonly used with filter() to apply conditions to elements.

Here's an example that demonstrates using lambda with filter():

lambda_filter.py
1# List of numbers
2numbers = [1, 2, 3, 4, 5]
3
4# Use filter() to get even numbers
5even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
6
7print(even_numbers)
Output
[2, 4]

In this example, filter() uses the lambda function lambda x: x % 2 == 0 to filter out even numbers from the list numbers.

Using Lambda with sorted()

The sorted() function returns a new sorted list from the elements of any iterable. You can use a lambda function as the key argument to specify custom sorting criteria.

Here's an example that demonstrates using lambda with sorted():

lambda_sorted.py
1# List of tuples
2students = [('Alice', 85), ('Bob', 92), ('Charlie', 78)]
3
4# Sort students by their grades in descending order
5sorted_students = sorted(students, key=lambda student: student[1], reverse=True)
6
7print(sorted_students)
Output
[('Bob', 92), ('Alice', 85), ('Charlie', 78)]

In this example, sorted() uses the lambda function lambda student: student[1] to sort the list of tuples by the second element (grades) in descending order.

When to Use Lambda vs. def

While lambda functions are powerful and concise, they have some limitations:

  • Readability: For complex logic, using a named function defined with def is more readable and maintainable.
  • Reusability: Named functions can be reused multiple times throughout your code, whereas lambda functions are typically used once and throwaway.

Here's a table summarizing when to use lambda versus def:

Use CaseLambda Functiondef Function
Short, simple logicYesNo
Temporary, one-off functionsYesNo
Complex logicNoYes
Reusable codeNoYes
Readability for othersNoYes

In general, use lambda functions for short and simple operations that are not reused elsewhere. For more complex or reusable logic, define a named function using def.

Practical Example

Let's create a complete program that demonstrates the use of lambda functions with map(), filter(), and sorted().

lambda_practical.py
1# List of numbers
2numbers = [10, 23, 45, 67, 89]
3
4# Use map() to square each number
5squared_numbers = list(map(lambda x: x ** 2, numbers))
6
7# Use filter() to get even numbers
8even_numbers = list(filter(lambda x: x % 2 == 0, squared_numbers))
9
10# Sort even numbers in descending order
11sorted_even_numbers = sorted(even_numbers, key=lambda x: x, reverse=True)
12
13print("Original Numbers:", numbers)
14print("Squared Numbers:", squared_numbers)
15print("Even Squared Numbers:", even_numbers)
16print("Sorted Even Squared Numbers:", sorted_even_numbers)
Output
Original Numbers: [10, 23, 45, 67, 89]
Squared Numbers: [100, 529, 2025, 4489, 7921]
Even Squared Numbers: [100, 2025, 4489]
Sorted Even Squared Numbers: [4489, 2025, 100]

In this practical example, we first square the numbers using map(), then filter out the even squared numbers using filter(), and finally sort them in descending order using sorted().

Summary

  • Lambda functions are anonymous functions defined with the lambda keyword.
  • They are useful for short, throwaway functions and can be used with higher-order functions like map(), filter(), and sorted().
  • Use lambda functions when you need a simple function temporarily; otherwise, use named functions defined with def for better readability and reusability.

What's Next?

In the next tutorial, we'll explore Python namespaces and scope, including global and local variables. Understanding these concepts will help you manage variable lifetimes and access in your programs more effectively. Stay tuned!


PreviousFunction Arguments (*args, **kwargs)Next Python Namespace & Scope (Global/Local)

Recommended Gear

Function Arguments (*args, **kwargs)Python Namespace & Scope (Global/Local)