Tuesday, 4 August 2026

๐Ÿš€ Day 95/150 – Lambda Function Examples in Python



๐Ÿš€ Day 95/150 – Lambda Function Examples in Python

A lambda function is a small, anonymous function in Python. It is useful when you need a simple function for a short period without defining it using the def keyword.

The syntax of a lambda function is:

lambda arguments: expression

In this post, we'll explore four common examples of lambda functions in Python.


Method 1 – Simple Lambda Function

Create a lambda function to add two numbers.

add = lambda a, b: a + b print(add(5, 3))



Output

8
Explanation
  • lambda a, b: defines an anonymous function with two parameters.

  • a + b is the expression whose result is returned automatically.

  • add(5, 3) returns 8.

Method 2 – Lambda with map()

Use a lambda function with map() to square each element in a list.

numbers = [1, 2, 3, 4, 5] squares = list(map(lambda x: x ** 2, numbers)) print(squares)






Output
[1, 4, 9, 16, 25]

Explanation

  • map() applies the lambda function to every element in the list.

  • lambda x: x ** 2 returns the square of each number.

  • list() converts the result into a list.


Method 3 – Lambda with filter()

Use a lambda function to filter even numbers from a list.


numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers)







Output
[2, 4, 6]

Explanation

  • filter() keeps only the elements for which the lambda function returns True.

  • lambda x: x % 2 == 0 checks whether a number is even.

  • The result is converted into a list.


Method 4 – Lambda with sorted()

Sort a list of tuples based on the second element.

students = [ ("Alice", 85), ("Bob", 92), ("Charlie", 78) ] sorted_students = sorted(students, key=lambda student: student[1]) print(sorted_students)









Output
[('Charlie', 78), ('Alice', 85), ('Bob', 92)]

Explanation

  • sorted() sorts the list.

  • The key parameter specifies the sorting rule.

  • lambda student: student[1] tells Python to sort using the second element (marks).


Comparison of Methods

MethodBest For
Simple LambdaShort mathematical operations
map()Transforming every element
filter()Selecting elements based on a condition
sorted()Custom sorting

๐Ÿ”ฅ Key Takeaways

  • A lambda function is a small anonymous function written in a single line.

  • It is best suited for short and simple operations.

  • map() uses lambda functions to transform data.

  • filter() uses lambda functions to select matching elements.

  • sorted() uses lambda functions to define custom sorting rules.

  • For complex logic, use a regular function (def) instead of a lambda function.

Stay tuned for Day 96 of the #150DaysOfPython series! ๐Ÿš€



0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (328) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (318) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (34) data (10) Data Analysis (43) Data Analytics (31) data management (16) Data Science (413) Data Strucures (18) Deep Learning (211) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (12) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (371) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1353) Python Coding Challenge (1208) Python Mathematics (8) Python Mistakes (51) Python Quiz (589) Python Tips (98) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (54) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)