Lambda Functions and List Comprehensions in Python
1. Lambda Functions in Python
Lambda functions are anonymous (unnamed) functions in Python, typically used for small operations.
Syntax
lambda arguments : expression
Example
square = lambda x : x * x
print ( square (5) ) # Output : 25
Key Points
• Anonymous: no function name
• Single expression only
• Often used with map(), filter(), sorted()
Practice Questions
1. (Beginner) Write a lambda to cube a number.
2. Use a lambda with map() to square all numbers in a list.
3. Use filter() and lambda to select even numbers from a list.
4. Sort a list of tuples by the second element using lambda.
5. Write a lambda to find the maximum of two numbers.
6. Convert a list of lowercase strings to uppercase using map() + lambda.
7. Create a dictionary of math ops (add, sub, mul) using lambdas.
1
2. List Comprehensions in Python
List comprehensions offer a concise way to create lists from iterables.
Syntax
[ expression for item in iterable if condition ]
Example
squares = [ x * x for x in range (5) ]
print ( squares ) # Output : [0 , 1 , 4 , 9 , 16]
Key Points
• Compact alternative to for-loops
• Supports conditions and nested loops
• Often used for data transformation
Practice Questions
1. (Beginner) Create a list of squares from 1 to 10.
2. Generate a list of even numbers from 0 to 20.
3. From a sentence, create a list of words longer than 4 letters.
4. Flatten a 2D list into 1D.
5. Create list of tuples (x, y) for x, y in 1 to 3.
6. Replace odd numbers in a list with "odd".
7. Convert Celsius temps in a dictionary to Fahrenheit.
2
3. Lambda Function Solutions
1. Cube a number:
cube = lambda x : x **3
print ( cube (3) ) # 27
2. Square all list elements:
nums = [1 , 2 , 3]
squares = list ( map ( lambda x : x **2 , nums ) )
print ( squares ) # [1 , 4 , 9]
3. Filter even numbers:
nums = [1 , 2 , 3 , 4]
evens = list ( filter ( lambda x : x % 2 == 0 , nums ) )
print ( evens ) # [2 , 4]
4. Sort tuples by second value:
data = [(1 , 3) , (2 , 1) , (4 , 2) ]
sorted_data = sorted ( data , key = lambda x : x [1])
print ( sorted_data ) # [(2 , 1) , (4 , 2) , (1 , 3) ]
5. Max of two numbers:
maximum = lambda a , b : a if a > b else b
print ( maximum (4 , 9) ) # 9
6. Convert to uppercase:
words = [ ’ apple ’ , ’ banana ’]
upper = list ( map ( lambda x : x . upper () , words ) )
print ( upper ) # [ ’ APPLE ’, ’ BANANA ’]
7. Lambda in a dictionary:
ops = {
’ add ’: lambda x , y : x + y ,
’ sub ’: lambda x , y : x - y ,
’ mul ’: lambda x , y : x * y
}
print ( ops [ ’ mul ’ ](3 , 4) ) # 12
3
4. List Comprehension Solutions
1. Squares from 1 to 10:
squares = [ x **2 for x in range (1 , 11) ]
print ( squares )
2. Even numbers from 0 to 20:
evens = [ x for x in range (21) if x % 2 == 0]
print ( evens )
3. Words longer than 4 chars:
sentence = " Python is a powerful programming language "
words = [ w for w in sentence . split () if len ( w ) > 4]
print ( words )
4. Flatten a 2D list:
matrix = [[1 , 2] , [3 , 4]]
flat = [ item for row in matrix for item in row ]
print ( flat )
5. Tuples (x, y) from 1 to 3:
pairs = [( x , y ) for x in range (1 , 4) for y in range (1 , 4) ]
print ( pairs )
6. Replace odd with ”odd”:
nums = [1 , 2 , 3 , 4]
result = [ x if x % 2 == 0 else " odd " for x in nums ]
print ( result )
7. Convert Celsius to Fahrenheit:
temps_c = { ’ day1 ’: 0 , ’ day2 ’: 20}
temps_f = { k : v *9/5 + 32 for k , v in temps_c . items () }
print ( temps_f )