`lambda`

is the root of a function, and acts very much like one with a small exception: implicit return.

```
f = lambda x: x ** 2
```

`f`

is the name we have given our lambda, and `x`

is the parameter. The expression on the right of the colon is the return.

```
print (f(6)) # 36
```

`sorted()`

takes an iterable which can be made up of lists or tuples of equal length, plus there are two flags of note to us, one is `reverse`

, as in `reverse=False`

(the default) and `key`

, which is where we use our lambda.

Eg.

```
>>> sorted([[2, "pepperoni"], [6, "pineapple"], [1, "cheese"], [3, "sausage"], [2, "olives"], [7, "anchovies"], [2, "mushrooms"]], key=lambda x: x[0])
```

Where `x`

is the outer list element, and we used the lambda to identify which element in the inner element to sort by. Since the numbers are the first value, then we select element `x[0]`

This gives us,

```
a = [[1, 'cheese'], [2, 'pepperoni'], [2, 'olives'], [2, 'mushrooms'], [3, 'sausage'], [6, 'pineapple'], [7, 'anchovies']]
>>>
```

The final step would be to take a slice, length 3 from the returned list. This is where we employ the slicing technique we learned in the list unit.

```
three_cheapest_slices = a[:3]
```

One should note that we didn’t actually need the key since it defaults to index 0 of the inner iterables, so we can write it similar to the way @lisalisaj did in her `.sort()`

method. That method is *in place* so cannot be assigned, hence using `sorted()`

which sorts a copy that is then assigned. This method does not alter the original list.

Where the `key`

would be useful is if we wanted to sort the list by names, not numbers.

```
>>> sorted([[2, "pepperoni"], [6, "pineapple"], [1, "cheese"], [3, "sausage"], [2, "olives"], [7, "anchovies"], [2, "mushrooms"]], key=lambda x: x[1])
[[7, 'anchovies'], [1, 'cheese'], [2, 'mushrooms'], [2, 'olives'], [2, 'pepperoni'], [6, 'pineapple'], [3, 'sausage']]
>>>
```

We can use the same technique for sorting `dict.items()`

by key or by value. Over time you will see and use this a lot so don’t let it throw you off. It is as simple as it looks, just takes some practice to commit to memory.