Mastering Python: Part 1: Lambda

Learning Python

Using Python

Mastering Python

Python is a general-purpose interpreted programming language. It emphasizes readability through the use of whitespace and supports multiple programming paradigms such as functional and object-oriented models.


Usually, functions have names in Python. Through using the def keyword, a function can be named and then called using its name.

Beyond the normal named function, there also exists a type of anonymous function in Python called a lambda.

A lambda is created using the keyword and then as many arguments as wanted. The list of the data sent into the special function stops at a colon. After this, there can only be a single expression statement.

In Python, a lambda is written using a single line. Unlike other functions where lines are “under” (spaced away from the left margin), a lambda only ever has a single expression statement as part of it.

With filter()

While lamdba may seem to be used in mathematical contexts, it can also be paired with the function filter() to parse something like a List.

As a type of function, a lambda can be written as part of working with a filter() usage to test as a series of entries. As a function with a single expression statement that returns a result, it can be used as a replacement for written a new function.

Play with the example on!