Certified Associate in Python Programming (PCAP-31-03) — Question 35
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))
Answer options
- A. 4
- B. 1
- C. an exception is raised
- D. 16
Correct answer: B
Explanation
The code filters the range from -2 to 2 for values whose absolute value is less than 1. The only value that meets this criterion is 0, resulting in a list with one element, hence the length of the list is 1. Other options do not reflect the correct count of elements that satisfy the filter condition.