Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Lambda in Python

Lambda in Python

Lambda in Python

In Python, a lambda function is a small anonymous function defined using the lambda keyword. Unlike regular functions, which are defined using the def keyword, lambda functions are used for short, throwaway functions that are typically one-liners.

Syntax of a Lambda Function

The basic syntax of a lambda function is:

lambda arguments: expression
  • lambda: The keyword that defines the function.

  • arguments: The parameters (can be zero or more) that the lambda function takes.

  • expression: A single expression that the lambda function computes and returns.

Unlike regular functions defined using def, a lambda function does not have a name and can be passed around in expressions and functions.

Examples of Lambda Functions

1. Basic Lambda Function

# Lambda function to add 10 to the inputadd_ten = lambda x: x + 10print(add_ten(5))  # Output: 15

Here, lambda x: x + 10 is a function that adds 10 to the input x.

2. Lambda with Multiple Arguments

# Lambda function to multiply two numbersmultiply = lambda x, y: x * yprint(multiply(3, 4))  # Output: 12

This lambda function takes two arguments, x and y, and returns their product.

3. Lambda with No Arguments

# Lambda function with no argumentssay_hello = lambda: "Hello, world!"print(say_hello())  # Output: Hello, world!

Here, the lambda function does not take any arguments and simply returns a string.

4. Using Lambda with map()

map() is a built-in Python function that applies a function to all items in an iterable (like a list).

# Using lambda with map to square each element in a listnumbers = [1, 2, 3, 4, 5]squared = list(map(lambda x: x ** 2, numbers))print(squared)  # Output: [1, 4, 9, 16, 25]

In this example, the lambda function squares each number in the list.

5. Using Lambda with filter()

filter() is used to filter out elements from an iterable based on a condition.

# Using lambda with filter to get even numbersnumbers = [1, 2, 3, 4, 5, 6]even_numbers = list(filter(lambda x: x % 2 == 0, numbers))print(even_numbers)  # Output: [2, 4, 6]

Here, the lambda function filters out all even numbers from the list.

6. Using Lambda with sorted()

You can use lambda to specify a custom sorting order for lists.

# Sorting a list of tuples based on the second valuepairs = [(1, 'one'), (3, 'three'), (2, 'two')]sorted_pairs = sorted(pairs, key=lambda x: x[1])print(sorted_pairs)  # Output: [(1, 'one'), (2, 'two'), (3, 'three')]

In this case, the lambda function sorts the list of tuples based on the second value (the string).


Advantages of Lambda Functions

  1. Concise: Lambda functions are written in a single line and are more compact than regular functions.

  2. Anonymous: They don’t require a name (though they can be assigned to a variable).

  3. Functional Programming: Lambda functions are used extensively in functional programming, especially in functions like map(), filter(), reduce(), etc.

  4. Inline use: You can use lambda functions inline wherever a function is required, making the code more readable and reducing the need for defining a separate function.


Limitations of Lambda Functions

  1. Single Expression: A lambda function can only contain one expression. It cannot have multiple statements like a regular function.

  2. Less Readable: For more complex operations, using lambda functions can make the code harder to read and understand. In such cases, using regular functions defined with def is often better.


When to Use Lambda Functions?

  • When you need a short function for a specific task, especially when it's passed as an argument to functions like map(), filter(), or sorted().

  • When you want to write clean and concise code for simple operations.

  • When you don't need to reuse the function, so there's no need to define it explicitly with def.

Conclusion

Lambda functions are a powerful tool for writing concise and functional-style code. However, they should be used for small, simple operations. For more complex logic, it's better to use regular functions for better readability and maintainability.

Let me know if you need more examples or further explanations!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql