Python Lambda / Anonymous Function

1) Basic Lambda

Create a lambda that adds 5 to a number and print result for input 10.


2) Two-Argument Lambda

Write a lambda to multiply two numbers. Test it with 3 and 4.


3) Square Lambda

Use a lambda to compute the square of a number.


4) Lambda in Variable

Assign a lambda to a variable f that returns x + 10. Call f(5).


5) Lambda in print()

Use a lambda inside a print() to double 7.


6) Lambda with map()

Given list [1, 2, 3, 4], use map + lambda to add 1 to each and print the new list.


7) Lambda with filter()

From the list [1,2,3,4,5], use filter + lambda to get only even numbers.


8) Lambda Sort Key

Sort the list of tuples [(1,3),(2,1),(3,2)] by second value using lambda.


9) Lambda with reduce()

Use functools.reduce + lambda to compute the product of the list [1, 2, 3, 4].


10) Double Values with Map

Use map + lambda to double each number in [3, 5, 7, 9].


11) Filter Odd Numbers

From [2,3,4,5,6] use filter + lambda to get only odd numbers.


12) Lambda Returning a Boolean

Create a lambda that checks if a number is > 10. Test with 7 and 15.


13) Lambda With Default Input

Write a lambda expression that adds two numbers but supply default values if none given.


14) Nested Lambda Call

Define a lambda that returns another lambda; use it to multiply numbers.

Example pattern:


15) Increment with Lambda

Use a lambda in a loop to increment each number in a list.


16) Lambda With String

Use a lambda to capitalize a string. Test with "hello".


17) Tuple Transformation

Use map + lambda to turn [(1,2),(3,4)] into [3,7] (sum of pair).


18) Filter Strings

From ["hi", "hello", "ok"], use filter + lambda to keep only strings with length > 2.


19) Lambda Sort Strings

Sort ["cat","apple","dog"] by length using lambda.


20) Combined Map & Filter

Given [1,2,3,4,5,6], first filter even numbers, then double them using lambda + map.

Expected pattern:


🧠 Concepts Practiced

Topic
Example Use

Basic lambda

lambda x: x+5

Two–arg lambda

lambda x,y: x*y

Map

Transform lists

Filter

Select elements

Reduce

Aggregate values

Sort key

Custom ordering

Nested lambda

Lambdas returning lambdas


📌 Quick Snippet Examples

Add 5 to 10

Map + Lambda

Filter + Lambda


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated