Skip to content

Question 25

What is the output of the following code?

1
2
3
4
def multiplier(list, k):
    return [(i*k) for i in list]

print(multiplier((lambda a:[i for i in range(a)])(6), 3))
Solution

multiplier() basically multiplies each element inside list by k, and putting them in a new list. For example, multiplier([1, 2, 3], 2) will multiply each element inside that list by 2, returning [2, 4, 6] at the end.

Now, let's break down multiplier((lambda a:[i for i in range(a)])(6), 3).

If we look at the first element passed into this multiplier() function call, it is a lambda function that returns a list. Specifically, lambda a: [i for i in range(a)] takes in a parameter a and then produces a list containing integers from 0 to before a. In our case, we have 6 passed as a parameter, so through the lambda function's iteration to produce the list, it returns [0, 1, 2, 3, 4, 5].

We can now say that the question is asking for multiplier([0, 1, 2, 3, 4, 5], 3), which is the result of multiplying each item in that list by 3, giving [0, 3, 6, 9, 12, 15].

Answer
[0, 3, 6, 9, 12, 15]