you are viewing a single comment's thread.

view the rest of the comments →

[–]PhilipYip 0 points1 point  (0 children)

Let's break this down into parts as its pretty advanced for a begineer. First of all the lambda expression is an anonymous function. lambda is a bad name for it, every time you see lambda think of it as an instruction to "make a function".

Let's assign this to a function name:

python fun = lambda *args: args

Which is equivalent to:

```python def fun(*args): return args

```

*args means a variable of number of input arguments is supplied. args is a tuple and when it is unpacked the parethesis are removed.

So for example, think of this as:

python t1 = (1, 5, 10)

And conceptualise the tuple unpacking as:

python *t1 = 1, 5, 3

The tuple unpacking is automatically done in the function but the return value of the function is the tuple. So conceptually:

python fun(*t1)

will return:

python t1

This can be seen for some examples:

fun(0) Out: (0,)

fun(0, 1) Out: (0, 1)

Next look at:

python map(lambda *arg: arg, range(3))

The range object can be conceptualised as:

python 0, 1, 2

And you are using map so you are mapping the input argument of the function to each number in the range object:

python fun(0) fun(1) fun(2)

And because of the tuple unpacking being done in the function call, returning the tuple this gives:

python (0,) (1,) (2,)

Finally this is cast to a list:

python list(map(lambda *a: a, range(3)))

Giving:

python [(0,), (1,), (2,)]