Python Lambda Functions

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will embark on an adventure to the mysterious island of Bennist where a renowned lost treasure hunter is on a quest to find the long-lost treasure of the island. The scenario will involve using Python lambda functions to help our treasure hunter navigate through the island's challenges and ultimately locate the elusive treasure.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") subgraph Lab Skills python/lambda_functions -.-> lab-271566{{"`Python Lambda Functions`"}} end

Lambda Function Basics

In this step, we will introduce the basics of lambda functions. We will learn how to create simple lambda functions and use them to perform basic operations. Additionally, we will explore the usage of lambda functions as arguments to other functions.

In /home/labex/project/lambda_basics.py:

## In this step, we will create a simple lambda function that adds two numbers
addition = lambda x, y: x + y
print(addition(3, 5))  ## Output: 8

Run the script:

python lambda_basics.py

The information below should be displayed on your terminal:

8

Lambda Functions with Filter

In this step, we will explore the usage of lambda functions with Python's built-in filter() function. We will demonstrate how lambda functions can be used to perform filtering and transformation operations on lists.

In /home/labex/project/lambda_filter.py:

## In this step, we will use a lambda function with filter to filter even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  ## Output: [2, 4, 6, 8, 10]

Run the script:

python lambda_filter.py

The information below should be displayed on your terminal:

[2, 4, 6, 8, 10]

Advanced Lambda Function Techniques

In this step, we will delve into advanced techniques for using lambda functions. We will explore scenarios where lambda functions shine, such as in sorting and key extraction.

In /home/labex/project/lambda_function.py:

## In this step, we will use a lambda function for custom sorting
points = [(1, 2), (5, 1), (3, 3), (7, 2)]
sorted_points = sorted(points, key=lambda x: x[1])
print(sorted_points)  ## Output: [(5, 1), (1, 2), (7, 2), (3, 3)]

Run the script:

python lambda_function.py

The information below should be displayed on your terminal:

[(5, 1), (1, 2), (7, 2), (3, 3)]

Summary

In this lab, we have explored the powerful capabilities of Python lambda functions. We have learned how lambda functions can be used for concise and efficient expression of functions, particularly in scenarios involving small, single-use functions. Additionally, we have seen how lambda functions can be employed with built-in functions like filter() and sorted() to streamline certain data processing tasks. This lab provides a solid foundation for utilizing lambda functions effectively in Python programming.

Other Python Tutorials you may like