Used together, the
filter function and
lambda form a concise means of testing each element of a sequence to determine whether or not that element is to be included in a new sequence that is being created. That is the focus of the Lambda Expressions exercise. For the test, the
False. A list comprehension is useful for a situation wherein the issue, rather than being a question of inclusion or non-inclusion, is primarily a mapping from a value in an original sequence to another value to be placed in a new sequence. For example, we can make a
list of cubes as follows ...
cubes = [n ** 3 for n in range(1, 21)]
The mapping is a transformation from one value to another. In the above case it is cubing the original number.
We could also include a test in a list comprehension. For example, if we only want cubes for which the final digit is not a
1, we could do this ...
cubes = [n ** 3 for n in range(1, 21) if n ** 3 % 10 != 1]
[8, 27, 64, 125, 216, 343, 512, 729, 1000, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000]
Alternatively, we could use the
filter function and a
lambda here to perform the test of the final digit, however that would be less concise for this example.