ZetCode

Python lambda functions

last modified July 6, 2020

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.

lambda_fun_simple.py
#!/usr/bin/env python3

def square(x):
    
    return x * x

sqr_fun = lambda x: x * x

print(square(3))
print(sqr_fun(4))

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.

$ ./lambda_fun_simple.py 
9
16

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.

lambda_fun_map.py
#!/usr/bin/env python3

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

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

for num in nums_squared:
    print(num)

The example creates a little 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 
1
4
9
16
25
36

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.

lambda_fun_filter.py
#!/usr/bin/env python3

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

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

print(nums_filtered)

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 sort

Python lists have a built-in list.sort() method that modifies the list in-place. The method has a key parameter to specify a function to be called on each list element prior to making comparisons. There we can use a lambda function.

lambda_fun_sort.py
#!/usr/bin/env python3


users = [
  {'name': 'John Doe', 'date_of_birth': 1987},
  {'name': 'Jane Doe', 'date_of_birth': 1996},
  {'name': 'Robert Brown', 'date_of_birth': 1977},
  {'name': 'Lucia Smith', 'date_of_birth': 2002},
  {'name': 'Patrick Dempsey', 'date_of_birth': 1994}
]

users.sort(reverse=True, key=lambda e: e['date_of_birth']) 

for user in users:
    print(user)

We have a list of user dictionaries. With the lambda function, we sort the users by their date of birth in the reverse order.

$ ./lambda_fun_sort.py 
{'name': 'Lucia Smith', 'date_of_birth': 2002}
{'name': 'Jane Doe', 'date_of_birth': 1996}
{'name': 'Patrick Dempsey', 'date_of_birth': 1994}
{'name': 'John Doe', 'date_of_birth': 1987}
{'name': 'Robert Brown', 'date_of_birth': 1977}

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 functions for the command parameter.

lambda_tkinter.py
#!/usr/bin/env python3


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
        
        self.initUI()
        
        
    def initUI(self):
      
        self.parent.title("Buttons")

        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()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    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.

List all Python tutorials.