“Unleashing the beast in Python” — We are Pythonistas, let’s write lighter codes.

I am powerful

Pythonic In-line functions:

There are a lot of built-in functions in Python which were efficiently designed and written just to make our lives easier. Most of the functions are used as an in-line functions at the time of writing out scripts. As we know, everything that we see in an OO language like Python are objects, belong to various classes.

Lambda function:

The Lambda function or lambda(), very well known as the anonymous function is designed specially to fit functions in a single line. We can definitely understand the power of this feature in terms of code readability, reduction in bulkiness of code and increase in efficiency.

Bulkier code using the traditional approach
Similar objective in two line of code using lambda()

Map function:

We all are familiar with how algebraic functions perform basic operations on a given set of input known as the Domain and which in turn maps on to another set as output known as the Co-domain. The Map function or map() in Python is a built-in function performs the similar way as it takes each item from any form of data structure as input and maps a function and generates a sequence of output.

Map function with Lambda

Filter function:

The Filter function or filter() is also one of the most important built-in functions Python hold. Just like normal filters in any other fields, in Python, the Filter function performs on a given set of conditions. It generate iterators from a given set of values if the passed conditions are satisfied.

Filtering with a bulky code
Similar code as above but in one line using filter() function

Pythonic approach— List comprehension

Being lazy is good but writing small and smart codes in Python is better. Python has given power to some of it’s data structures such as the List and the most reputed among them is the power of List comprehension. A shorter syntax is one of the most important phase of a good programming language, mostly in the world of Data Science where you don’t have much time to write codes.

For loop structure with lengthy codes
Similar code but in one line

--

--

Hello World 👋. I work as a Business Analyst 📊. I love exploring Data Science 📈, listening to Metal 🎧 and playing riffs on my guitar 🎸.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adipta Biswas

Hello World 👋. I work as a Business Analyst 📊. I love exploring Data Science 📈, listening to Metal 🎧 and playing riffs on my guitar 🎸.