# Python Lambda: a small function inline

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-lambda-full-en
`lambda args: expression` makes an unnamed function whose body is one expression. It can be passed as the `key` argument of `list.sort` or `sorted`.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-74-A, TE-74-B and TE-74-C. The expected non-zero exit is TE-74-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-74-A: sorted with a lambda key, and a lambda assigned to a name

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
words = ['pear', 'fig', 'banana']
print(sorted(words, key=lambda w: len(w)))
add = lambda a, b: a + b
print(add(2, 3))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
['fig', 'pear', 'banana']
5
```

Exit status: 0. The output matches the expected text written before the run.

### TE-74-B: a lambda can be called on the spot, and map applies one to every item

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print((lambda x: x * 2)(4))
print(list(map(lambda x: x + 1, [1, 2])))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
8
[2, 3]
```

Exit status: 0. This status and the output match what was written before the run.

### TE-74-C: expected non-zero exit: calling a two-argument lambda with one argument raises TypeError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
f = lambda a, b: a + b
f(1)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b21-rqhcf5kl/work/s.py", line 2, in <module>
    f(1)
TypeError: <lambda>() missing 1 required positional argument: 'b'
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-74-B: the lambda called with 4 returned 8, and map with a lambda that adds 1 gave [2, 3]. TE-74-C: calling the two-argument lambda with one argument stopped with exit status 1. The last line of standard error is TypeError: <lambda>() missing 1 required positional argument: 'b'.

## Limits

A lambda holds one expression and no statements. Late binding of loop variables was not tested.

## Source

- https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions
- https://docs.python.org/3/library/functions.html#sorted

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-74-A` | sorted with a lambda key, and a lambda assigned to a name | 0 | PASS | `3d5f33c83061` |
| `TE-74-B` | a lambda can be called on the spot, and map applies one to every item | 0 | PASS | `e878a34df06d` |
| `TE-74-C` | expected non-zero exit: calling a two-argument lambda with one argument raises TypeError | 1 | PASS | `5f0eb4e8ef4d` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python Default Arguments: evaluated once](https://kairos-2z6s.onrender.com/res/python-default-arguments-full-en)
- [Python Generators: produce values one at a time](https://kairos-2z6s.onrender.com/res/python-generators-full-en)
- [Python lists: indexes, slices and the IndexError](https://kairos-2z6s.onrender.com/res/python-list-basics-full-en)
- [Python json: read JSON text into Python values](https://kairos-2z6s.onrender.com/res/python-json-full-en)
- [Python List Slicing: take part of a list](https://kairos-2z6s.onrender.com/res/python-list-slicing-full-en)
- [Python itertools.chain: loop over several sequences as one](https://kairos-2z6s.onrender.com/res/python-itertools-chain-full-en)
