Data Analysis

3 Amazing Reasons — Why To Use Lambda Function In Python

Lambda functions in Python are simple, concise, and flexible. Learn why to use them in your data analysis project!

Suraj Gurav
5 min readMay 29, 2023

3 cocktails representing three reasons to use python lambda functions in data analysis project.
Photo by Licor Beirão on Unsplash

Python is a commonly used language in data analysis.

It has made entire data processing and its transformation easy with a wide range of built-in functions and methods. Moreover, you can easily create user-defined functions.

Functions are small blocks of reusable code, which you define once and use as and when you need them.

However, sometimes you need a function only once and in such a situation you don’t always need to go through the whole hassle of defining a function.

Then what else you can do in such cases?

Use Lambda Functions!!! ✅

Lambda functions are anonymous functions i.e. functions without a name. In contrast to other user-defined functions, you can literally define and use them in the same line of code.

But, this is not the only reason to use it in your data analysis project.

In this story, I’m presenting 3 key reasons — why you should use Lambda functions in Python for data analysis.

To make it more interesting, I’ve provided three easy-to-follow examples on the way.

Let’s dive in!

As Lambda functions are anonymous, you never need to think about any name for them, which makes them simple. That’s where I’ve found the first reason to use it.

Simplicity — Lambda Functions are simple

Lambda functions are defined and used on the same line, making them simple to write and use.

Let’s take an example here.

Suppose you have a list of tuples and each tuple contains a string and a number. You now want to sort this list in ascending order based on a numeric value in each tuple.

You can easily achieve this using lambda functions as below.

my_list = [("are", 3), ("lambda", 1), ("simple", 4), ("functions", 2)]
sorted_list = sorted(my_list…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Suraj Gurav

Analytics professional and writer. I write about Data Science, Python, SQL & interviews. Join Medium today to get all my articles: https://tinyurl.com/3fehn8pw