How to use python Lambda Functions

A smiling blue robot opens a red toolbox against a pale green background, holding a silver spanner labelled “lambda” in black.

When you first learn Python, you will usually create functions with the def keyword. These are great when you need a block of code that you plan to reuse. However, Python also has a shorter way to create functions called lambda functions. These are sometimes called anonymous functions because they do not need a name.

What is a lambda function

A lambda function is a quick way of writing a small function in a single line. They are useful when you only need a function once or for a very short task.

The syntax is:

The word lambda starts the function, then you list the arguments, followed by a colon, and finally the expression that gets calculated and returned.

Basic example

Here is a simple lambda function that adds two numbers together:

This does the same thing as writing a normal function with def, but it is shorter.

Using lambda with strings

We can also use lambda functions with strings like “LiddleBit”. For example, turning a name into uppercase:

Lambda inside other functions

Lambda functions are often used together with built-in Python functions like map, filter, and sorted. For example, you can sort a list of names by their length:

Here the lambda tells Python to sort by the length of each name instead of the default alphabetical order.

When to use lambda functions

Lambda functions are best for small, quick tasks where writing a full function would feel like too much. For anything longer or more complicated, stick to the regular def style so your code stays clear and easy to read.

Things to try yourself

  1. Write a lambda function that squares a number and test it with different values.
  2. Use map with a lambda function to turn a list of names, including “LiddleBit”, into lowercase.
  3. Use filter with a lambda function to find numbers greater than 10 in a list.
  4. Sort a list of words by their last letter using sorted with a lambda.

Lambda functions may look unusual at first, but they are simply a quicker way to write short, throwaway functions. Once you get used to them, you will see how handy they can be in many everyday Python tasks.

Main Topic

Python Functions

A flat, landscape-style digital illustration shows a man in an orange shirt drinking from a glass of water. A speech bubble next to him contains the function call drink_water() in bold monospaced text.

A beginner-friendly post explaining Python functions, highlighting clarity, reusability, collaboration, and scalability benefits.

Other Tutorials in this Topic

Illustration showing Python function syntax with def, a function name, parentheses, and indented placeholder code.

Defining Python Functions

Beginner-friendly post introducing Python functions, explaining their syntax, naming conventions, and usage, with simple examples and practice…

Two cartoon robots smiling as one passes a yellow box labelled “age” to the other.

Passing Arguments to Python Functions

A beginner-friendly Python tutorial explaining functions, arguments, keyword arguments, and default values, with examples, readability tips, and…

Three smiling robots on a blue background, each smaller than the last, passing a yellow box with speech bubble showing process().

Recursion in Python

Beginner-friendly Python guide introducing recursion, explaining base cases, benefits, and examples like countdown and string reversal, with…