Python Lambda Functions: A Beginner's Guide

1 0 0
                                    

 Python Lambda Functions: A Beginner's Guide

Lambda functions in Python are small, anonymous functions defined using the 'lambda' keyword. They are often used for short-term operations where defining a full function would be overkill. Here's a guide to help you understand and use lambda functions effectively.

1. Basic Syntax

A lambda function has the following syntax:

'''python

lambda arguments: expression

'''

- 'lambda': This keyword is used to declare a lambda function.

- 'arguments': This is the list of arguments (parameters) the function takes.

- 'expression': This is a single expression evaluated and returned by the function.

Example:

'''python

square = lambda x: x 2

print(square(5)) Output: 25

'''

2. Multiple Arguments

Lambda functions can take multiple arguments, separated by commas.

Example:

'''python

add = lambda x, y: x + y

print(add(3, 5)) Output: 8

'''

3. Using Lambda Functions with 'map()'

The 'map()' function applies a given function to all items in an input list (or any iterable) and returns a map object (an iterator).

Example:

'''python

numbers = [1, 2, 3, 4]

squared = list(map(lambda x: x 2, numbers))

print(squared) Output: [1, 4, 9, 16]

'''

4. Using Lambda Functions with 'filter()'

The 'filter()' function constructs an iterator from elements of an iterable for which a function returns true.

Example:

'''python

numbers = [1, 2, 3, 4, 5, 6]

evens = list(filter(lambda x: x % 2 == 0, numbers))

print(evens) Output: [2, 4, 6]

'''

5. Using Lambda Functions with 'reduce()'

The 'reduce()' function from the 'functools' module applies a rolling computation to sequential pairs of values in a list.

Example:

'''python

from functools import reduce

numbers = [1, 2, 3, 4]

product = reduce(lambda x, y: x y, numbers)

print(product) Output: 24

'''

6. Sorting with Lambda Functions

Lambda functions can be used as the key for sorting.

Example:

'''python

points = [(1, 2), (3, 1), (5, -1), (2, 3)]

sorted_points = sorted(points, key=lambda x: x[1])

print(sorted_points) Output: [(5, -1), (3, 1), (1, 2), (2, 3)]

'''

7. Lambda Functions in List Comprehensions

You can use lambda functions within list comprehensions to perform operations.

Example:

'''python

numbers = [1, 2, 3, 4, 5]

squared = [(lambda x: x 2)(x) for x in numbers]

print(squared) Output: [1, 4, 9, 16, 25]

'''

Conclusion

Lambda functions in Python are a powerful tool for writing short, anonymous functions that can be used in various functional programming constructs. They are particularly useful for small operations and for use with functions like 'map()', 'filter()', and 'reduce()'. While lambda functions can make your code more concise, they should be used judiciously to maintain code readability.

You've reached the end of published parts.

⏰ Last updated: Aug 07 ⏰

Add this story to your Library to get notified about new parts!

Python Lambda Functions: A Beginner's GuideWhere stories live. Discover now