Anonymous functions?

Revisiting the Power of Lambda λ Functions in Python

Enhancing Your Python Skills: Lambda functions Recap!

CyCoderX
4 min readJun 4, 2024

In a previous article, we explored Lambda functions in Python. Now, let’s do some revision and dive deeper into their practical applications in data engineering.

Image by Thought Catalog on unsplash

Recap: What Are Lambda Functions?

Lambda functions, also known as anonymous functions, are small, unnamed functions defined using the lambda keyword. They are designed for single or short-term use where defining a full function would be overkill.

The syntax of a lambda function is:

lambda arguments: expression

Here’s a simple example of a lambda function that doubles a number:

lambda x: x + 2

This Lambda function takes an argument x and returns x multiplied by 2. The syntax is succinct, making it perfect for scenarios where you need a quick, throwaway function.

Why Use Lambda Functions?

Lambda functions in Python offer several key benefits:

  • Conciseness: Lambda functions are short and concise. They can be defined in a single line, making them useful for short operations.
  • Anonymous Functions: They don’t require a name, making them ideal for functions that are used temporarily or in a limited scope.
  • Reduced Boilerplate: Eliminates the need for formal function definitions, reducing boilerplate code.
  • Event Handling: Useful in GUI programming for attaching simple callbacks or handlers without cluttering the namespace.
  • Readability: When used appropriately, they can make your code more readable by encapsulating functionality inline.
  • Functional Programming: They fit well with functional programming paradigms, allowing the creation of higher-order functions and quick functional manipulations, such as using functions as arguments to other functions.

Basic Examples:

# A function that takes in (x), then returns (x + 1)
def add1(x):
return x + 1

# Equivalent lambda function
lambda x: x + 1



# A function that takes in (x, y), then returns (x + y)
def add(x, y):
return x + y

# Equivalent lambda function
lambda x, y: x + y



# A function that takes in (x, y, z), then returns (x + y + z)
def add(x, y, z):
return x + y + z

# Equivalent lambda function
lambda x, y, z: x + y + z



# A function that takes in NOTHING, then returns 123
def some_function():
return 123

# Equivalent lambda function
lambda : 123

Practical Applications in Data Engineering

Lambda functions are often used with functions like map(), filter(), sorted() and reduce() to efficiently manipulate data.

1. Data Transformation

One common task in data engineering is transforming data. Suppose you have a list of numbers and want to filter out numbers greater than 3, then square the remaining ones:

numbers = [1, 2, 3, 4, 5, 6]
filtered_and_squared = list(map(lambda x: x**2, filter(lambda x: x <= 3, numbers)))
print(filtered_and_squared) # Output: [1, 4, 9]

2. Sorting Data

Lambda functions are particularly useful for custom sorting. Consider you have a list of tuples representing employees and you want to sort them by their salary:

employees = [
("Alice", 70000),
("Bob", 55000),
("Charlie", 80000)
]
sorted_employees = sorted(employees, key=lambda x: x[1])
print(sorted_employees)

3. Reducing Data

The reduce() function from the functools module is useful for applying a rolling computation to sequential pairs of values in a list. Here’s an example to find the maximum value in a list of numbers:

from functools import reduce

numbers = [1, 2, 3, 4, 5]
max_value = reduce(lambda x, y: x if x > y else y, numbers)
print(max_value) # Output: 5

Note: Python’s built-in functions such as max() are often more readable and efficient alternatives for many common use cases.

4. Lambda Functions in Pandas

The Pandas library often leverages Lambda functions for various operations. For example, you can filter rows in a DataFrame using the apply function in conjunction with a Lambda function:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df_filtered = df[df['Age'].apply(lambda x: x > 28)]
print(df_filtered)

5. Inline Functions in Generators

Lambda functions can also be embedded within generators for more complex transformations:

cubed_gen = ((lambda x: x**3)(x) for x in range(5))
for num in cubed_gen:
print(num) # Output: 0, 1, 8, 27, 64

6. Inline Functions in List Comprehensions

Lambda functions can also be embedded within list comprehensions for more complex transformations:

cubed = [(lambda x: x**3)(x) for x in range(5)]
print(cubed) # Output: [0, 1, 8, 27, 64]
Image by Andrew Neel on unsplash

Conclusion

Lambda functions in Python offer a powerful tool that can help you write compact and succinct code. They are particularly useful for tasks like data filtering, custom sorting and working in conjunction with libraries such as Pandas. By incorporating Lambda functions into your coding practices, you can significantly enhance your efficiency and productivity.

As someone who specializes in data engineering, I’ve found that a thorough understanding of Lambda functions is invaluable for developing code that is not only cleaner but also more optimized. I highly recommend delving into the capabilities of Lambda functions to discover how they can improve your coding projects.

Final Words:

Hey There! I’m Charilaos Alkiviades Savoullis, a data engineer who loves crafting end-to-end solutions. I write articles about Python, Databases and SQL, interviews, lifestyle and more!.

Thank you for taking the time to read my article.

For similar articles and updates, feel free to explore my Medium profile https://medium.com/@casavoullis

If you enjoyed this article, consider liking and following for future updates!

Join me as we explore the exciting world of tech, data and beyond!

Don’t hesitate to connect with me on my socials:

LinkedIn: https://bit.ly/3UK8KNk
GitHub: https://bit.ly/3WrMzgm

Are you also interested in Python content? Click here to check out my list on Medium.

Let me know your thoughts in the comments!

--

--

CyCoderX
CyCoderX

Written by CyCoderX

Machine Learning & Data Engineer | Data Science | Python & SQL | AI | Software Developer | Azure & AWS Cloud Specialist | Blogger simplifying Big Data & Trends

No responses yet