Lambda Expressions, Map, Filter & Reduce Functions In Python : Part 1

Expressiveness, elegance, and readability are just few goodies of Python’s selling points. Anyone with intermediate mastery of python’s concepts and constructs can express in python 5 to 10 lines what would have taken 15 to 30 lines in other programming languages. It’s not in my interest to start a technology war against other programming languages. Personally, I strongly believe there is no such thing as the best programming language. Every language has what it is able to do best and it takes knowledge, experience and wisdom to choose the right technology stack for the right job. Languages like Python and Ruby are highly expressive and can be used to implement awesome solutions with proper engineering discipline.

If the only tool you have is a hammer, every problem appears to you as a nail. However, hammers can’t be used to trim facial hairs, scissors can’t be used to cut iron rods. Learn enough technologies to know when to choose what, period.

In this post and subsequent ones, my aim is to demonstrate how expressive python is through it’s lambda expressions, and map, filter, & reduce functions.

What’s In A Lambda Expression?

Lambdas are simple one-line functions which are able to accept arbitrary number of parameters and are also able to return values when called. Note however that the def and return keywords are inferred in lambda expressions therefore we do not have to include them when creating lambdas.

Lambdas expressions are lynchpins in all functional programming languages. Lambdas allow for functions and blocks of code to be passed around as though they were common values. With lambdas, we are able to pass functions as parameters to other functions, return functions from functions, store functions into variables etc.

The snippets below shows a simple function and its equivalent lambda expression.

Function vrs Lambda Expression In Python

Lambda expressions have this general form

name = lambda param1,param2,…,paramN : return expression. For example, to create a lambda that returns the product of 3 numbers you’ll have to do it this way product = lambda x,y,z : x * y * z

The variable product holds the definition of the lambda expression and can be used to invoke the lambda as though it were a regular function. You can invoke the product lambda by product(2,6,12) and store the return value in a variable name of your choice (of course, it should be descriptive and meaningful)

Note that you are free use values of any type including custom classes as input parameters to the lambda expression. Also, you can return values of any type from lambda expressions.

So When & How Do I Use Lambda Expressions?

At this point, you may be wondering how lambdas are useful aside saving you some few keystrokes on the keyboard. Hang on buddy, hang on! Once we begin to talk about the map, filter and reduce functions in my next stories you’ll see the light and the usefulness of lambda expressions will be embraced forever.

Thanks for your time. Leave your comments, suggestions, corrections, criticisms etc here and don’t forget to follow me for more lessons.

I’ll talks about map, filter, and reduce functions in subsequent posts.