Python lambda functions

Python lambda functions tutorial shows how to create anonymous functions in Python. Anonymous functions in Python are created with lambda keyword.

Python lambda functions

Python lambda functions, also known as anonymous functions, are inline functions that do not have a name. They are created with the lambda keyword. This is part of the functional paradigm built-in Python.

Python lambda functions are restricted to a single expression. They can be used wherever normal functions can be used.

Python lambda syntax

Python lambda has the following syntax:

z = lambda x: x * y

The statement creates an anonymous function with the lambda keyword. The function multiplies two values. The x is a parameter that is passed to the lambda function. The parameter is followed by a colon character. The code next to the colon is the expression that is executed when the lambda function is called. The lambda function is assigned to the z variable.

Python lambda example

The following is a simple example demonstrating Python lambda function.


def square(x):
    return x * x

sqr_fun = lambda x: x * x


In the example, we have two functions that square a value.

def square(x):
    return x * x

This is a Python function defined with the def keyword. The function's name is square.

sqr_fun = lambda x: x * x

Here we define an anonymous, inline function with lambda. Note that the function does not have a name. The sqr_fun is a name of the variable that holds the created lambda function.


This is the output of the example.

Python lambda with map

Python lambda functions are useful with the map() function. We can create more concise code. Python map() is a built-in function which applies the given function on every item of iterable(s) and returns an iterator object.


nums = [1, 2, 3, 4, 5, 6]

nums_squared = map(lambda x: x * x, nums)

for num in nums_squared:

The example creates a litle inline function for the map() as a parameter. With the map() function we apply the lambda function on each element of the list.

$ ./lambda_fun_map.py 

This is the output.

Python lambda with filter

Python lambda functions can be used with the filter() function. The filter() function constructs a list from those elements of the iterable for which the function returns true.


nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

nums_filtered = list(filter(lambda x: x % 2, nums))


In the example, we filter the list of integers. The new list contains only odd integers.

nums_filtered = list(filter(lambda x: x % 2, nums))

The first parameter of the filter() is the function which processes the list elements. The lambda function has the x % 2 expression, which returns true for odd values.

$ ./lambda_fun_filter.py 
[1, 3, 5, 7, 9, 11]

This is the output.

Python lambda with Tkinter

Python lambda function can be used in GUI programming with Tkinter. It allows to create small, inline functios for the command parameter.

# -*- coding: utf-8 -*-

from tkinter import Tk, BOTH, messagebox
from tkinter.ttk import Frame, Button

class Example(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
    def initUI(self):

        self.pack(fill=BOTH, expand=1)

        btn1 = Button(self, text="Button 1",
            command=lambda: self.onClick("Button 1"))
        btn1.pack(padx=5, pady=5)
        btn2 = Button(self, text="Button 2",
            command=lambda: self.onClick("Button 2"))
        btn2.pack(padx=5, pady=5)
        btn2 = Button(self, text="Button 3",
            command=lambda: self.onClick("Button 3"))
        btn2.pack(padx=5, pady=5)   
    def onClick(self, text):
        messagebox.showinfo("Button label", text);

def main():
    root = Tk()
    app = Example(root)

if __name__ == '__main__':

We have three buttons that share one callback. The lambda function allows us to send specific data to the callback function. Each button displays its label in a message box.

btn1 = Button(self, text="Button 1",
    command=lambda: self.onClick("Button 1"))

We pass an anonymous function to the command parameter. We send the label of the button to the onClick() callback.

In this tutorial, we have worked with the Python lambda functions.

You might also be interested in the following related tutorials: Python tutorial, Python set tutorial, Python for loop, Python list comprehensions, Python map tutorial, Openpyxl tutorial, Python requests tutorial, and Python CSV tutorial.