QuestionQ32

VerifiedMiscellaneous (List Comprehensions, Lambdas, Closures, I/O)

What is the expected output of the following code?

myli = range(-2, 2)  
m = list(filter(lambda x: True if abs(x) < 1 else False, myli))  
print(len(m))  
Explanation

range(-2, 2) yields -2, -1, 0, and 1. Only 0 satisfies abs(x) < 1, so the filtered list contains one element and len(m) prints 1.

Community Discussion

No comments yet. Be the first to start the discussion!