Introduction

In Python, anonymous functions, also known as lambda functions, are an incredibly powerful tool that can be used to create small, one-time use functions without having to define them formally with a def statement. These functions can be used in a variety of applications, such as sorting and filtering data in lists, creating simple mathematical operations, and much more. In this tutorial, we will explore the syntax and use cases of anonymous functions in Python and how they can be leveraged to make coding more efficient and concise. So, if you're interested in understanding and using anonymous functions in Python, let's get started!

Table of Contents :

  • What are Anonymous Functions
  • Anonymous Functions in Python
    • Passing Anonymous functions as arguments
  • Where can we use Anonymous function
    • sort a list of data by some criteria
    • create small throwaway functions on the fly

What are Anonymous Functions :

  • Anonymous functions are functions that are not bound to a name
  • Anonymous functions are often used in situations where 
    • the function will be used only once
    • or a regular function would require too much boilerplate code
  • Anonymous functions can be used anywhere a regular function can be used. 

Anonymous Functions in Python

  • Python supports the creation of anonymous functions at runtime, using a special syntax. 
  • In Python anonymous functions are defined using the  lambda  keyword.
  • The function body is specified after the keyword, just like a regular function.
  • Below we have is a simple example of an anonymous function in Python 
  • This anonymous function takes two arguments and returns their sum: 
  • Sample Code : 

f = lambda x, y: x + y
s = f(1, 2)
print(s)

# Output
# 3 

Passing Anonymous functions as arguments :


def apply_func(x, func):
    return func(x)


s = apply_func(2, lambda x: x * x)
print(s)

# Output
# 4 

Where can we use Anonymous function :

  • There are endless possibilities of using anonymous functions.
  • We have listed a few use cases where we can use anonymous functions.
    • sort a list of data by some criteria
    • create small throwaway functions on the fly
    • delay the execution of a function

sort a list of data by some criteria

  • One common use case for anonymous functions is to sort a list of data by some criteria. 
  • For example, let's say you have a list of strings and you want to sort them by length. You can do this using the sorted() function and an anonymous function: 

strings = ['foo', 'bar', 'ba', 'q', 'quux']
sorted_strings = sorted(strings, key=lambda s: len(s))
print(sorted_strings)

# Output
# ['q', 'ba', 'foo', 'bar', 'quux']


  • As you can see, the sorted() function 
    • takes an optional key argument, which should be a function 
    • This function takes a single argument and returns a value that will be used for sorting. 
  • In the above example, we use an anonymous function that returns the length of the string, so the list is sorted by string length. 

create small throwaway functions on the fly

  • Another common use case for anonymous functions is to create small throwaway functions on the fly. 
  • For example, let's say you want to square all the numbers in a list: 

numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x * x, numbers)
print(list(squared))

# Output
# [1, 4, 9, 16, 25] 

  • The map() function applies a function to every element in a sequence and returns a list of the results. 
  • In the above example, we use an anonymous function that squares each number. 
  • Of course, we can achieve the same result with a regular function.But in this case, using an anonymous function is more convenient

Prev. Tutorial : Nested functions

Next Tutorial : Lambda expressions